Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a place name in google map in android

I have a requirement that whenever I drag/browse the Google map and place a marker on it, it should display the name of the place(where the marker is placed) in an AutocompleteTextview. I have checked out the Places API and the Geocoder class but i least understood it. I need to do the task same as that of the Places tab in the life360 app. How can I achieve it?

like image 822
void Adi Avatar asked Feb 28 '14 13:02

void Adi


People also ask

How do I show a name marker on Google Maps?

For that click on the style option just below layer name. Now change the set label option to name or description to display it.


2 Answers

try this it may help you...

When u mark a place, try to capture latitude & longitude with marker & use this code to get location name

public String getAddress(Context context, double lat, double lng) {
    Geocoder geocoder = new Geocoder(context, Locale.getDefault());
    try {
        List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
        Address obj = addresses.get(0);

        String add = obj.getAddressLine(0);
        add = add + "\n" + obj.getCountryName();
        add = add + "\n" + obj.getCountryCode();
        add = add + "\n" + obj.getAdminArea();
        add = add + "\n" + obj.getPostalCode();
        add = add + "\n" + obj.getSubAdminArea();
        add = add + "\n" + obj.getLocality();
        add = add + "\n" + obj.getSubThoroughfare();

        return add;
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
        return null;
    }
}
like image 57
Anil kumar Avatar answered Sep 30 '22 14:09

Anil kumar


Try this I hope Its work.

 public void getLocation(double lat, double lng) {
    Geocoder geocoder = new Geocoder(MapsActivity.this, Locale.getDefault());       

    try {
        List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
        Address obj = addresses.get(0);
        String add = obj.getAddressLine(0);           

        Log.e("IGA", "Address" + add);


    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }

}
like image 38
Abhinav singh Avatar answered Sep 30 '22 14:09

Abhinav singh