0

In my project I used Google Maps Activity and to get the user's current location I used setMyLocationEnabled(this); method to get the location. And when I tried to fetch the latitude and longitude from that location it throws a error says null object reference.

I looked for many videos in YouTube and examined many questions in stack overflow and blogs but I can't find the proper code to be done to do that. Some use deprecated methods and some go way beyond the topic.

So all I want is to do the reverse geo coding to get the address from my location and also add a marker at that point.

Help me guide what to do and how to obtain that I'm stuck with that from past 4 days.

S.Ambika
  • 292
  • 1
  • 14

2 Answers2

0

Reverse Geocoding is what you are looking for. Geocoder class can be used Look at this Android: Reverse geocoding - getFromLocation

It will give you idea of using reverse geocoding

S.Ambika
  • 292
  • 1
  • 14
0

You can use getFromLocation from Geocoder.

Sample code.

Geocoder geocoder = new Geocoder(context, Locale.getDefault());
try {
    List<Address> addressList = geocoder.getFromLocation(
            latitude, longitude, 1);
    if (addressList != null && addressList.size() > 0) {
        Address address = addressList.get(0);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
            sb.append(address.getAddressLine(i)).append("\n");
        }
        sb.append(address.getLocality()).append("\n");
        sb.append(address.getPostalCode()).append("\n");
        sb.append(address.getCountryName());
        String result = sb.toString();
    }
} catch (IOException e) {
    Log.e(TAG, "Unable connect to Geocoder", e);
}
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128