最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

When using the Google Maps Places API you can create an Autoplete element attached to a map in order to search their library.

When you select a place you can call autoplete.getPlace() to retrieve a JSON object describing the place. One field of this is the place_id, 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?

When using the Google Maps Places API you can create an Autoplete element attached to a map in order to search their library.

When you select a place you can call autoplete.getPlace() to retrieve a JSON object describing the place. One field of this is the place_id, 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?

Share Improve this question asked Mar 3, 2015 at 17:46 M1keM1ke 6,4064 gold badges35 silver badges52 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

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.

发布评论

评论列表(0)

  1. 暂无评论