从纬度和经度获取地址

时间:2016-11-24 10:50:42

标签: android google-maps reverse-geocoding

我想获取点击地图的位置地址。我尝试使用地理编码器。但它不起作用。这需要任何API。请帮助我。我将在下面发布我用过的代码。

   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")));


    }
});

3 个答案:

答案 0 :(得分:0)

```

                    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是您的位置纬度和经度

答案 1 :(得分:0)

这非常简单,只需要在http请求下调用它,它将返回可以轻松解析的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"
}

答案 2 :(得分:0)

你可以试试吗,

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