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

痴心易碎 提交于 2019-11-30 07:21:16
Soham

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.

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

Both of the proposed ways cost money and I think peoples should mention it. We have the -$200 from the Google, but its all depends from how much users you have and how frequent you need to use the geocode. 60k requests per month to the geocoder - easy $300 only for to use the geocode https API :

Nov 1 – 30, 2018 Geocoding API Geocoding: 6073 Counts - $30.38

Dont filter what you need in the responses, like we have in the requests from other posts? Expect way more:

Nov 1 – 30, 2018 Places API Places Details: 2389 Counts $40.61

Nov 1 – 30, 2018 Places API Contact Data: 2385 Counts $7.17

Nov 1 – 30, 2018 Places API Atmosphere Data: 2385 Counts $11.95

I do recommend to use get the Place Id like this. It would be better than call the API directly. I stumble for this issue all day. so I hope that it would be help. who're finding the way out to get place ID from your current location. for android

Kotlin

val mPlaceDetectionClient = Places.getPlaceDetectionClient(this.context!!)  
val placeResult =  mPlaceDetectionClient.getCurrentPlace(null) 
placeResult.addOnCompleteListener(object : OnCompleteListener<PlaceLikelihoodBufferResponse>{
    override fun onComplete(p0: Task<PlaceLikelihoodBufferResponse>) {
        val likelyPlaces = p0.result
        if(likelyPlaces.any()){
            // get the place ID from code below
            placeId = likelyPlaces[0].place.id  }
    }

})

or if who are implementing or developing on another language such a java. you can implement your code according to a reference below.

java

https://developers.google.com/places/android-sdk/googleapi-migration

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