简体   繁体   中英

How to get Place ID using coordinates (lat, lon) in Android

I use Google Maps API and need to get details of the place using lat and lon.

I've tried to get via Geocoder, but Class " Address " has no parameters PLACE_ID

Geocoder gcd = new Geocoder(getApplicationContext(), Locale.getDefault());
            List<Address> addresses = null;
            try {
                addresses = gcd.getFromLocation(52.2641, 76.9597, 1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (addresses.size() > 0) {
                Toast.makeText(this, addresses.get(0).getPlaceID(), Toast.LENGTH_SHORT).show();
            }
            else {
                // do your stuff
            }

FYI

Geocoder did't provide getPlaceID() .

Supply a placeId to find the address for a given place ID. The place ID is a unique identifier that can be used with other Google APIs

The GeocoderRequest object literal contains the following fields:

{
 address: string,
 location: LatLng,
 placeId: string,
 bounds: LatLngBounds,
 componentRestrictions: GeocoderComponentRestrictions,
 region: string
}

You should read Retrieving an Address for a Place ID .

 /**
   * Requests the details of a Place.
   *
   * <p>We are only enabling looking up Places by placeId as the older Place identifier, reference,
   * is deprecated. Please see the <a
   * href="https://web.archive.org/web/20170521070241/https://developers.google.com/places/web-service/details#deprecation">
   * deprecation warning</a>.
   *
   * @param context The context on which to make Geo API requests.
   * @param placeId The PlaceID to request details on.
   * @return Returns a PlaceDetailsRequest that you can configure and execute.
   */
  public static PlaceDetailsRequest placeDetails(GeoApiContext context, String placeId) {
    PlaceDetailsRequest request = new PlaceDetailsRequest(context);
    request.placeId(placeId);
    return request;
  }

you can get place Id on the bases of the marker. something like this:

   mGoogleMap.setOnPoiClickListener(new GoogleMap.OnPoiClickListener() {
        @Override
        public void onPoiClick(PointOfInterest pointOfInterest)
        {
                addOrSaveMarkerToMap(pointOfInterest.latLng.latitude, pointOfInterest.latLng.longitude, pointOfInterest.name, "", pointOfInterest.placeId, true, true);


            //Toast.makeText(GoogleMapsActivity.this,""+pointOfInterest.name+" (lat: "+pointOfInterest.latLng.latitude+", long: "+pointOfInterest.latLng.longitude+") " +" is added in your favorite list",Toast.LENGTH_LONG).show();
        }
    });

If you want to read more about what is point of interest is please take a look at this documentation link.

https://developers.google.com/maps/documentation/android-sdk/poi

Best way is to use google places API . It will give you the whole information about a specific place.

If you want to use google places API then follow this link:

How to get place or placeid by latLng in android using google places api?

To get a place ID from coordinate you should execute a reverse geocoding lookup using a REST API.

The native Android API geocoder doesn't support getting place ID in getFromLocation() results. Unfortunately, Google Maps Android SDK doesn't provide built-in Geocoder neither. The feature request exists for a long time, but it looks like it doesn't have high priority:

https://issuetracker.google.com/issues/35823852

So, to obtain a place ID you are stick to the REST API. There is a Java client library for Google Maps API Web Services that you can find on github:

https://github.com/googlemaps/google-maps-services-java

You can use this library to call Geocoding API from your Java code.

GeoApiContext context = new GeoApiContext.Builder()
    .apiKey("AIza...")
    .build();
GeocodingResult[] results =  GeocodingApi.newRequest(context)
    .latlng(new LatLng(52.2641, 76.9597)).await();
System.out.println(results[0].placeId);

Note that API key for web services must be different from an API key that you use in Google Maps Android SDK, because web services don't support Android app restriction.

Also take into account this important note from the library documentation

The Java Client for Google Maps Services is designed for use in server applications. This library is not intended for use inside of an Android app, due to the potential for loss of API keys.

If you are building a mobile application, you will need to introduce a proxy server to act as intermediary between your mobile application and the Google Maps API Web Services. The Java Client for Google Maps Services would make an excellent choice as the basis for such a proxy server.

I hope this helps!

try this one

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());

addresses = geocoder.getFromLocation(latitude, longitude, 1); 
// Here 1 represent max location result to returned, by documents it recommended 1 to 5

String address = addresses.get(0).getAddressLine(0); 
// If any additional address line present than only, check with max 
available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName(); 
// Only if available else return NULL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM