本文介绍了Google Maps API v3 Places库返回未定义的utc_offset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个时间和位置感知应用程序,并且google maps v3 api库库几乎包含了我需要的所有内容。但是,当针对特定地址执行textSearch()并尝试为其中一个返回的PlaceResult项目调用getDetails()时,getDetails()返回的PlaceResult.utc_offset属性未定义(我需要的所有其他PlaceResult属性均返回正确,而不是utc_offset)。
这很奇怪,因为如果我为像pizza这样的通用术语执行textSearch(),然后为返回的PlaceResult项目之一调用getDetails(),则PlaceResult从getDetails()返回的.utc_offset属性将有一个值。
有谁知道为什么utc_offset是为通用搜索的结果填充的,但不是在搜索具体的地址,或者我做错了什么? 解决方案
。我所做的是,如果它是未定义的,我打电话给Google TimeZone API来获取信息。我已经在下面提供了一个代码示例,但您也可以在GitHub上看到我的解决方案, p>
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
$ b $ .get(timeZoneApiRequestUrl,function(data){
var utcOffset =(data.rawOffset + data.dstOffset || 0)/ 3600;
console.log(UTC offset is+ utcOffset +hours)
}); / /扩大这个来处理错误,只是做一个成功的现在作为一个POC
}
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?
解决方案
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() +
"×tamp=" + 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
}
这篇关于Google Maps API v3 Places库返回未定义的utc_offset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!