我如何从Android的Lat Long获取地址?

问题描述 投票:1回答:4

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

现在我正在变长,我在函数中解析了Lat长来获取地址。

这里是代码

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();
    }
}
android google-maps reverse-geocoding
4个回答
3
投票

尝试一下,它将为您提供帮助。

我已经编辑了代码。可能是您的设备可能不支持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

            }
  }

0
投票

这是给定here的相同问题的答案的代码片段>

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);

String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);

0
投票

使用此:


0
投票

在此处传递您的纬度和经度值。

© www.soinside.com 2019 - 2024. All rights reserved.