如何从Lat Long获取地址在android中?

时间:2014-11-27 04:33:52

标签: android google-maps reverse-geocoding

我正在构建一个Android应用程序,用户使用谷歌地图选择该区域。

现在我得到lat long,我在一个函数中解析Lat long来获取地址。

这是代码

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();
    }
}

4 个答案:

答案 0 :(得分:2)

试试这个,它会对你有帮助。

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

            }
  }

答案 1 :(得分: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);

答案 2 :(得分:0)

使用此:

private void getAddressFromLocation(final LatLng latlng) {


        new Thread(new Runnable() {

            @Override
            public void run() {

                Geocoder gCoder = new Geocoder(this);
                try {
                    final List<Address> list = gCoder.getFromLocation(
                            latlng.latitude, latlng.longitude, 1);
                    if (list != null && list.size() > 0) {
                        Address address = list.get(0);
                        StringBuilder sb = new StringBuilder();
                        if (address.getAddressLine(0) != null) {
                            sb.append(address.getAddressLine(0)).append("\n");
                        }
                        sb.append(address.getLocality()).append(",");
                        sb.append(address.getPostalCode()).append(",");
                        sb.append(address.getCountryName());
                        String strAddress = sb.toString();

                    }

                } catch (IOException exc) {
                    exc.printStackTrace();
                }
            }
        }).start();

    }

这里已经回答:https://stackoverflow.com/a/22324307/2655804

答案 3 :(得分:0)

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

    Geocoder geocoder;
    List<Address> addresses;
    geocoder = new Geocoder(getContext(), Locale.getDefault());

    try {
        addresses = geocoder. getFromLocation(latitude, longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
        String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
        String city = addresses.get(0).getLocality();
        String state = addresses.get(0).getAdminArea();
        String country = addresses.get(0).getCountryName();
        String postalCode = addresses.get(0).getPostalCode();
        String knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL

        System.out.println(address+"-------------");
    } catch (IOException e) {
        e.printStackTrace();
    }
相关问题