I noticed the other day that my usage of the Google Maps Timezone API was failing. I realised this was down to me not passing in an API key with the call. In their attempts to monetise their Maps API, Google now requires the API key and each call is chargeable. So I added the key and it still didn’t work, although with a different error message. Apparently using a key with a HTTP referrer restriction wasn’t allowed.
So I decided to add a server-side handler on my server that called out to the Timezone API using a server key instead. This was fairly straightforward since it just bounced the AJAX request from the browser to the Timezone API URL.
I checked back the next day to see what my API usage looked like. I’d spent $5 in 24 hours. Continuing with that meant with my other Maps API usage I’d hit the $200 per month free limit and would have to start paying Google money again, something I’ve been loath to do since their ridiculous price increases*
I realised at that point that the Timezone API wasn’t actually doing a huge amount behind the scenes. I guessed there would be libraries out there that could do the same thing but without paying for the privilege. Turns out there is. GeoTimeZone will give the time zone ID for a location and TimeZoneConverter will convert that to a Windows TimeZoneInfo that gives me everything else I needed to build my own version of the Timezone API. The code to do that is something like this
var lat = double.Parse(HttpContext.Current.Request.QueryString["lat"]);
var lng = double.Parse(HttpContext.Current.Request.QueryString["lng"]);
var tz = TimeZoneLookup.GetTimeZone(lat, lng).Result;// get other info
var tzi = TZConvert.GetTimeZoneInfo(tz);// write out as JSON
var jsonObj = new JObject();
var rawOffset = tzi.BaseUtcOffset.TotalSeconds;
jsonObj["dstOffset"] = tzi.GetUtcOffset(DateTime.UtcNow).TotalSeconds - rawOffset;
jsonObj["rawOffset"] = rawOffset;
jsonObj["timeZoneId"] = tz;
jsonObj["timeZoneName"] = tzi.StandardName;
jsonObj["status"] = "OK";var json = JsonConvert.SerializeObject(jsonObj);
HttpContext.Current.Response.Write(json);
The only thing to consider is that time zones change so it’s worth keeping the two packages up to date.
* For the record, I used to pay about $200 a month to Google. Now I pay about the same to here maps and nothing to Google. I’m intrigued to know how their new pricing has worked out for them, I’m assuming most websites would have made the same decision I did and moved somewhere else.
No comments:
Post a Comment