In my previous blog, I wrote about getting lat-long coordinates from address. In this blog, I'm going to write about getting timezone from lat-long coordinates. The authentication process remains same for both the process. So both blogs will have same content in the authentication section except for API name.

Step 1 - Authentication

Sign up at Google developer console and create a project. Create Project

You can give a name of your choice and project ID will provided by Google. Project Name

Go to Enable an API section to enable APIs of your choice Enable API

In our case, we should enable Time Zone API and it provides 2,500 requests per day for free.

Now go to Credentials under APIs & auth. Click Create new Key. Create key

Select Server key in the popup. Server key

If you want to restrict access to this key for certain IP addressess, you can do so in the next screen. Otherwise leave the textbox blank and click Create. Enter IP

Now copy the API key in this screen. Copy key

Step 2 - Install dependencies

We can user python requests package to make requests to the API. You can install the package using the command

pip install requests

Step 3 - Make requests

You can make requests to the API using the following code:

import time
import requests

api_key = "<api key copied from google>"
latitude  = 37.4224764
longitude = -122.0842499
timestamp = time.time()

api_response = requests.get('{0},{1}&timestamp={2}&key={3}'.format(latitude,longitude,timestamp,api_key))
api_response_dict = api_response.json()

if api_response_dict['status'] == 'OK':
    timezone_id = api_response_dict['timeZoneId']
    timezone_name = api_response_dict['timeZoneName']
    print 'Timezone ID:', timezone_id
    print 'Timezone Name:', timezone_name

The output of this function will be

Timezone ID: America/Los_Angeles
Timezone Name: Pacific Daylight Time

The full successful response from the API will be of the following format:

{u'dstOffset': 3600,
 u'rawOffset': -28800,
 u'status': u'OK',
 u'timeZoneId': u'America/Los_Angeles',
 u'timeZoneName': u'Pacific Daylight Time'}

Now we have got timezone from lat-long coordinates in few simple steps. Please feel free to post your comments.

More information can be read from Google timezone API docs.