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

删除回忆录丶 提交于 2019-12-18 12:30:05

问题


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!


回答1:


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.




回答2:


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




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/34283484/how-to-get-placeid-from-name-or-lat-long-in-android

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