-1

I want to get the address of the location where the map is clicked.I tried using geocoder. But it is not working.Does this require any API.Please help me.I'll text my used code below.

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


    }
});
Aparna
  • 83
  • 1
  • 1
  • 8
  • Check my answer here: http://stackoverflow.com/a/32647204/3626214 – Aspicas Nov 24 '16 at 10:54
  • openstreetmap.org provides a similar function to reverse geocode. – Nakul Sudhakar Nov 24 '16 at 10:56
  • 2
    define: *But it is not working.* ... this code is extremly stupid ... `addresses = new ArrayList<>();` ??? what for the next line is `addresses = geocoder....` ... `addresses.get(0);` ?? what if addresses is empty ? ... why it is not behind `addresses = geocoder....` inside first try?? – Selvin Nov 24 '16 at 10:57

3 Answers3

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 = "";
                    }

```

where Lat,Lng are your locations latitude and longitudes

Ak9637
  • 990
  • 6
  • 12
0

It's very simple, just have to call below http request and it will return the JSON Response that can be parsed easily to get address.

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"
}
Chirag
  • 56,621
  • 29
  • 151
  • 198
0

can you try with this,

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

and other one,

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

and third option

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

Saveen
  • 4,120
  • 14
  • 38
  • 41
  • thanku Saveen for ur response..I applied ur first codes and for me location is which is returned is of 0 length.Following that I found out that geocoder backend is not present..I've used google places api. How can I rectify this? – Aparna Nov 25 '16 at 05:33
  • please recheck your latitude and longitude it shouldn't be null or 0. – Saveen Nov 25 '16 at 05:41
  • you can do with above api as well just pass latitude and longitude – Saveen Nov 25 '16 at 05:42