How to get PlaceID on behalf of or lat, long in Android?

This is 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 not find something like address.getPlaceID ().

Maybe I use another API to get PlaceID on behalf of or lat, the long code above. Thanks!

+8
source share
4 answers

Look at these docs . You will get the name lat, long, the name for Address and then call it 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 an answer like this

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

There you will receive a place identifier.

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

+7
source

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

See Getting Started with Geocoding

+3
source

Both of the proposed methods cost money, and I think that people should mention this. We have - $ 200 from Google, but it all depends on how many users you have and how often you need to use the geocode. 60 thousand requests per month for the geocoder - just $ 300 only for using the https geocode APIs:

November 1 - 30, 2018 Geocoding API Geocoding: 6073 columns - $ 30.38

Do not filter what you need in the responses, as in requests from other messages? Expect more:

November 1 - 30, 2018 Places API Place Details: 2389 Counts $ 40.61

November 1 - 30, 2018 Places API Contact Details: 2385 Counts $ 7.17

November 1 - 30, 2018 Places API Atmosphere Data: 2,385 graphs $ 11.95

0
source

I recommend using Place Id like this. This would be better than calling the API directly. I stumble over this problem all day. so i hope this helps. who find a way out to get a 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 someone implements or develops in another language, for example, Java . You can implement your code in accordance with the link below.

Java

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

0
source

All Articles