Google Maps API v3 Places Library returning undefined utc_offset

十年热恋 提交于 2020-01-02 07:58:40

问题


I'm developing a time and location aware application and the google maps v3 api places library has almost everything I need. However, when doing a textSearch() for specific addresses, and attempting to call getDetails() for one of the returned PlaceResult items, the PlaceResult.utc_offset property returned from getDetails() is undefined (all the other PlaceResult properties I need are returned fine, just not the utc_offset).

It's strange, because if I do a textSearch() for a generic term like "pizza" and then call getDetails() for one of the returned PlaceResult items, the PlaceResult.utc_offset property returned from getDetails() will have a value.

Does anyone know why the utc_offset is populated for the results of a generic search, but not when searching for a specific address, or what am I doing wrong?


回答1:


As you mentioned in only works when there are generic terms like "pizza". What I've done is if it is undefined, I call the Google TimeZone API to get the info. I've put a code sample below, but you can see my solution on GitHub as well, https://github.com/pushpickup/pushpickup/blob/master/client/views/games/creategame/select-location.js#L13

if (place.utc_offset === void 0) {
  timeStamp = (Date.now() || new Date().getTime())/1000;
  timeZoneApiRequestUrl = "https://maps.googleapis.com/maps/api/timezone/json?location=" +
                            place.geometry.location.lat() + "," + place.geometry.location.lng() +
                            "&timestamp=" + timeStamp + "&key=YOUR_API_KEY"

  $.get(timeZoneApiRequestUrl, function( data ) {
    var utcOffset = (data.rawOffset + data.dstOffset || 0) / 3600;
    console.log("UTC offset is " + utcOffset + " hours")
  }); // expand this to handle errors, just doing a get with success for now as a POC
}


来源:https://stackoverflow.com/questions/19717327/google-maps-api-v3-places-library-returning-undefined-utc-offset

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