Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get PlaceID from name, or lat, long in Android?

This my code:

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    List<Address> addresses = geocoder.getFromLocationName(text, 10);
    test.clear();
    for (int i = 0; i < addresses.size(); i++) {
        Address address = addresses.get(i);
        for (int j=0;j<address.getMaxAddressLineIndex();j++) {
            test.add(address.getAddressLine(j) + ", " + address.getCountryName());
//                address.getLatitude();
//                address.getLatitude();
            }
        }

But I can't find something like address.getPlaceID().

Maybe I use another API get PlaceID from name, or lat, long from above code. Thanks!

like image 799
rome 웃 Avatar asked Dec 15 '15 07:12

rome 웃


2 Answers

You also can use direct search instead of nearbysearch https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY

Look at Getting Started with Geocoding

like image 116
Yvgen Avatar answered Sep 29 '22 11:09

Yvgen


Look at this docs. You will get the lat,long,name for the Address and then call this below google api

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&name=cruise&key=YOUR_API_KEY

You will get a response like this

{
  "html_attributions" : [],
  "results" : [
    {
      "geometry" : {
        "location" : {
          "lat" : -33.870775,
          "lng" : 151.199025
        }
      },
      ...
      "place_id" : "ChIJrTLr-GyuEmsRBfy61i59si0",
      ...
    }
  ],
  "status" : "OK"
}

For there you will get the place-id.

Note: Please enable the GeoLocation Api from Google Developer Console otherwise it return the error msg :This API project is not authorized to use this API.

like image 45
Soham Avatar answered Sep 29 '22 10:09

Soham