Redwing19
Redwing19

Reputation: 85

Google Maps API v3 Places Library returning undefined utc_offset

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?

Upvotes: 3

Views: 1924

Answers (1)

nickytonline
nickytonline

Reputation: 6981

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
}

Upvotes: 1

Related Questions