我正在构建一个android应用程序,其中用户使用google map选择区域。

现在,我正在拉特长,并且在函数中解析了拉特长号以获得地址。

这是代码

public void getMyLocationAddress() {

    Geocoder geocoder= new Geocoder(getApplicationContext(), Locale.getDefault());
     List<Address> addresses = null;
    try {

          //Place your latitude and longitude
         addresses = geocoder.getFromLocation(mUpCameraPosition.target.latitude,mUpCameraPosition.target.longitude, 1);
          if(addresses != null) {

              Address fetchedAddress = addresses.get(0);
              StringBuilder strAddress = new StringBuilder();

              for(int i=0; i<fetchedAddress.getMaxAddressLineIndex(); i++) {
                    strAddress.append(fetchedAddress.getAddressLine(i)).append("\n");
              }

              completed = strAddress.toString();
              whitebord.setText(completed);

          }
             whitebord.setText("No location found..!");
    }
    catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
             Toast.makeText(getApplicationContext(),"Could not get address..!",
             Toast.LENGTH_LONG).show();
    }
}

最佳答案

试试这个,它将为您提供帮助。

我已经编辑了代码。可能是您的设备可能不支持Geocoder。使用Google API获取地址

try{
Geocoder geocoder = new Geocoder(SignIn.this,
                    Locale.getDefault());
            List<Address> addresses = geocoder.getFromLocation(mlatitiude,
                    mlongitude, 1);
            if (addresses.size() > 0) {
                Address address = addresses.get(0);
                mCountryName = address.getCountryName();
                mLocationName = address.getLocality();

            }
  } catch (IOException e) {
  HttpClient httpclient = new DefaultHttpClient();
            String postURL = "http://maps.googleapis.com/maps/api/geocode/json?latlng="
                    + mlatitiude + "," + mlongitude + "&sensor=true";
            HttpGet httppost = new HttpGet(postURL);
            try {

                HttpResponse response = httpclient.execute(httppost);
                BufferedReader rd = new BufferedReader(
                        new InputStreamReader(response.getEntity()
                                .getContent()));
                String line = "";
                StringBuilder sb = new StringBuilder();
                while ((line = rd.readLine()) != null) {
                    sb.append(line + "\n");
                }
                String result = sb.toString();

                JSONObject jobj = new JSONObject(result);
                JSONArray array = jobj.getJSONArray("results")
                        .getJSONObject(0)
                        .getJSONArray("address_components");
                int size = array.length();
                for (int i = 0; i < size; i++) {
                    JSONArray typearray = array.getJSONObject(i)
                            .getJSONArray("types");

                    if (typearray.getString(0).equals("country")) {
                        mCountryName = array.getJSONObject(i).getString(
                                "long_name");

                    }
                    if (typearray.getString(0).equals("locality")) {
                        mLocationName = array.getJSONObject(i).getString(
                                "long_name");

                    }

                }

            } catch (Exception e1) {
                // TODO: handle exception

            }
  }

关于android - 如何从Lat Long In android获取地址?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27163120/

10-12 00:20
查看更多