Android Question Getting time offset (time zone) from Lat & Long

SeaBee

Member
Licensed User
Apparently there is a Google Time Zone API that enables getting the GMT/UTC offset from latitude and longitude. I can find references to it but not much detail.

Is there a wrapper for this in B4A, or some kind of work-around?

Thanks,

Chris C-B
 

nwhitfield

Active Member
Licensed User
Longtime User
The documentation looks pretty straightforward (assuming you have an API key)

B4X:
https://maps.googleapis.com/maps/api/timezone/outputFormat?parameters

So, you need to register for a MAP api key. Request JSON, because it's easy to parse in B4A. Use DateTime.now to get a timestamp (and divide by 1000 to convert to seconds), then just call it as you would a normal URL request.

B4X:
dim job as httpjob
job.initialise("",Me)
dim apikey as string = "supersecretAPIkey"
dim loc as string = "51.52,-0.03"
job.url = "https://maps.googleapis.com/maps/api/timezone/json?location=" & loc & "&timestamp=" & ( Datetime.now/1000) & "&key=" & apikey
job.download

You'll get a response back in JSON, that's something like

B4X:
{
"dstOffset" : 3600,
"rawOffset" : 0,
"status" : "OK",
"timeZoneId" : "Europe/London",
"timeZoneName" : "British Summer Time"
}

Grab it using job.getstring, and then parse the JSON. Something like this

B4X:
wait for (job) JobDone as HttpJob
if job.success then
  dim parser as JSON parser
  parser.initialize(job.GetString)
  dim timezone as map = parser.NextObject

  log("Time zone name is " & timezone.get("timeZoneName"))
end if


However, also think about why and how you need to get this data, and what you're doing with it? For example, if the phone has network connectivity, it will most likely have updated to the correct local time. So as long as your app can get a UTC timestamp from a web service, then you can calculate the difference anyway. It would seem to me that the chief advantage of the Google API is that you can get the name, and you can find out the offset, even if the user hasn't allowed their phone to update time autoamatically - but then what are you using it for? If the user's decided, for instance, to keep their phone set to their home timezone, then using geolocation to find out where they are and display local timestamps might well not be what they want.
 
Upvote 0

SeaBee

Member
Licensed User
Many thanks for all the work you put into your most detailed reply - much appreciated. The problem I face is that I was hoping to avoid using the maps API, as the Google Maps licence agreement precludes storing the data, and as this app is a planning tool, it is essential to be able to store the data for future use. It may well be that the app is used in a location where there is no web connectivity ('phone or WiFi), just GPS. The problem exists when the user is planning from a different time zone to the one to which he will eventually travel. At the moment, the offset is entered manually, but I would have preferred to automate it. I have been using the app in Windows.net, where I had built my own gazetter with this information, but at over 800MB, it is too large for an Android 'phone.

Incidentally, I am also using the B4A geocode library without a 'supersecretAPIkey'. Will this stop working when published?

Thanks,

Chris C-B
 
Upvote 0

nwhitfield

Active Member
Licensed User
Longtime User
The Google terms do allow that "you may store limited amounts of Content solely for the purpose of improving the performance of your Maps API Implementation due to network latency" and not having connectivity would be a pretty extreme case of network latency, I should imagine?

You might also want to check https://timezonedb.com/ for an alternative service or http://www.geonames.org which also has a timezone web service

I don't know about the B4A geocode library; it doesn't appear to require an API key so you should be ok with that.
 
Upvote 0

SeaBee

Member
Licensed User
timezonedb.com looks like exactly what I need, as it is pretty compact. I used geonames.org to build up my own gazetteer for Windows, but it is BIG! I also implemented my own time zone table, but it was a nightmare to maintain, as countries seem to take delight in changing time zones and/or borders.

Thanks very much,

Chris C-B
 
Upvote 0
Top