Search code examples
androidgoogle-geocoder

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!


Solution

  • 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.