Getting a place name in google map in android

本小妞迷上赌 提交于 2020-01-22 20:09:28

问题


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?


回答1:


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



回答2:


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

}


来源:https://stackoverflow.com/questions/22096044/getting-a-place-name-in-google-map-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!