从纬度和经度获取地址

To obtain address from latitude and longitude

我想获取地图所在位置的地址 clicked.I 已尝试使用地理编码器。但这不是 working.Does 这需要任何 API.Please 帮助 me.I 将在下面发送我使用的代码。

   mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

    @Override
    public void onMapClick(LatLng point) {
        //save current location
        latLng = point;

        List<Address> addresses = new ArrayList<>();
        try {
            addresses = geocoder.getFromLocation(point.latitude, point.longitude,1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        android.location.Address address = addresses.get(0);

        if (address != null) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < address.getMaxAddressLineIndex(); i++){
                sb.append(address.getAddressLine(i) + "\n");
            }
            Toast.makeText(MapsActivity.this, sb.toString(), Toast.LENGTH_LONG).show();
        }



        //place marker where user just clicked
        marker = mMap.addMarker(new MarkerOptions().position(point).title("Marker")));


    }
});

```

                    String mAddress = "";
                    try {

                        //mAddress = new Geocoder(this).getFromLocation(Lat,Lng,1).get(0).getAddressLine(0).toString();
                        Geocoder geocoder = new Geocoder(this);
                        // mAddress = new Geocoder(this).getFromLocation(Lat,Lng,1).get(0).getAddressLine().toString();
                        for (int ai = 0; ai < 5; ai++) {

                            try {
                                mAddress = mAddress + "\n" + geocoder.getFromLocation(Lat, Lng, 1).get(0).getAddressLine(ai).toString();
                            } catch (Exception e) {
                                break;
                            }

                        }
                    } catch (Exception e) {
                        mAddress = "";
                    }

```

其中 Lat,Lng 是您所在位置的纬度和经度

很简单,只需要调用下面的http请求,它就会return得到JSON响应,很容易解析得到地址。

http://maps.googleapis.com/maps/api/geocode/json?latlng=23,72&sensor=true

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Gujarat State Highway 17",
               "short_name" : "GJ SH 17",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Babajipara",
               "short_name" : "Babajipara",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Ahmedabad",
               "short_name" : "Ahmedabad",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Gujarat",
               "short_name" : "GJ",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "363115",
               "short_name" : "363115",
               "types" : [ "postal_code" ]
            }
         ]
],
   "status" : "OK"
}

你能试试这个吗,

public String getCompleteAddress(double latitude, double longitude) {
    String location = "";
    try {
        Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
        List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
        if (addresses.size() > 0) {
            Address address = addresses.get(0);
            String state, city, zip, street;
            if (address.getAdminArea() != null) {
                state = address.getAdminArea();
            } else {
                state = "";
            }
            if (address.getLocality() != null) {
                city = address.getLocality();
            } else {
                city = "";
            }
            if (address.getPostalCode() != null) {
                zip = address.getPostalCode();
            } else {
                zip = "";
            }

            if (address.getThoroughfare() != null) {
                street = address.getSubLocality() + "," + address.getThoroughfare();
            } else {
                street = address.getSubLocality() + "," + address.getFeatureName();
            }
            location = street + "," + city + "," + zip + "," + state;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return location;
}

还有另外一个,

  public Address getAddressFromLocation(double latitude, double longitude){
    String zipcode="";
    Address address=null;
    try {
        Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
        List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);

        if (addresses.size() > 0) {
            address= addresses.get(0);
            String admin = address.getAdminArea();
            String subLocality = address.getSubLocality();
            String city = address.getLocality();
            zipcode = address.getPostalCode();

        }

    } catch (IOException e) {
        e.printStackTrace();
        mAppUtil.showToast("Please try again");


    }
 return address;
 }

和第三个选项

http://maps.googleapis.com/maps/api/geocode/json?latlng=30.7333,76.7794&sensor=true