Google Maps API v3 Returns the undefined site library utc_offset

I am developing an application for determining the time and location, and the google maps v3 api library has almost everything I need. However, when doing textSearch () for specific addresses and trying to call getDetails () for one of the returned PlaceResult elements, the PlaceResult.utc_offset property returned from getDetails () is undefined (all the other PlaceResult properties I need are returned fine, just not utc_offset).

This is strange because if I use textSearch () for a generic pizza term and then call getDetails () on one of the returned PlaceResult elements, the PlaceResult.utc_offset property returned from getDetails () will have a value.

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

+2
source share
1 answer

As you mentioned only in cases where there are general terms such as "pizza". What I did, if it is undefined, I call the Google TimeZone API to get the information. I put the sample code below, but you can also see my solution on 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
}
+1
source

All Articles