M1ke M1ke - 1 year ago 173
Javascript Question

How do I retrieve place details from the Google Places JS API using a place ID?

When using the Google Maps Places API you can create an

element attached to a map in order to search their library.

When you select a place you can call
to retrieve a JSON object describing the place. One field of this is the
, Google's unique reference. Google actually allow these to be stored:

Place IDs are exempt from the caching restrictions stated in Section 10.1.3 of the Google Maps APIs Terms of Service. You can therefore store place ID values indefinitely.

However it's not clear from the documentation how you can use this stored value to retrieve the details of a place. Are there any methods, and how should these be used?

Answer Source

There is an object in the google.maps.places namespace called PlacesService. This has a method called getDetails() which can accept an ID.

Confusingly the PlacesService object requires a HTML element to be created. If you just want to use getDetails you will not need to subsequently use this element, so you can just create one using whatever flavour you prefer - the example below uses jQuery:

var obj=$('<div>').appendTo('body');
var service=new google.maps.places.PlacesService(obj.get(0));
service.getDetails({placeId:''},function(PlaceResult, PlacesServiceStatus){
   // PlaceResult is an object
   // PlacesServiceStatus is a string

Again oddly, the object passed to getDetails is referred to as a PlaceDetailsRequest in the documentation, however this class doesn't exist, it's just a description for how the object should be formatted.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download