2013-10-31 43 views
2

我正在開發時間和位置感知應用程序,谷歌地圖v3 api地方庫幾乎包含我需要的所有東西。但是,當針對特定地址執行textSearch()並嘗試爲其中一個返回的PlaceResult項目調用getDetails()時,getDetails()返回的PlaceResult.utc_offset屬性未定義(我需要的所有其他PlaceResult屬性均返回正確,而不是utc_offset)。這很奇怪,因爲如果我爲像「pizza」這樣的通用術語做了一個textSearch(),然後爲返回的PlaceResult項之一調用getDetails(),getDetails()返回的PlaceResult.utc_offset屬性將有一個值。谷歌地圖API v3 Places庫返回undefined utc_offset

有誰知道爲什麼utc_offset填充了一般搜索的結果,但沒有搜索特定的地址,或者我做錯了什麼?

+0

你在哪裏看到utc_offset?我在[PlaceResult](https://developers.google.com/maps/documentation/javascript/3.exp/reference#PlaceResult) – geocodezip

+0

的文檔中沒有看到它請提供源代碼 – Brett

+0

你有沒有想到這個出來嗎?我有同樣的問題。 – nickytonline

回答

1

正如你所提到的,只有當有像「披薩」這樣的通用術語時纔有效。我所做的是,如果它是未定義的,我打電話給Google TimeZone API來獲取信息。我已經把一個代碼示例下面,但你可以看到我的解決方案在GitHub上爲好,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 
}