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
You can give a name of your choice and project ID will provided by Google.
![Project Name](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_uUEetlymroOVVjAH6Ecyn7SbM_7EdDZmQvofP7wuZaN9UNe4GtkBvfDWwODqXtrJBD97wtkYxnHJ7hxojN8maNWJf_4NfKBjKd8zCo1z5iIwGzaUhFWogyTw=s0-d)
Go to Enable an API section to enable APIs of your choice
![Enable API](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_vZ1HfOfQQ-NGmGMx3tzFPEpeizKSQ7TY4rA_0adGAc4aPBfFQlIzC9yG09kYj91hIKk98GM-QVY-wrlcQDwLp9U0jdVsdX6L3j_vHe5aJm5SwFQEJ-XSUzfg=s0-d)
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](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_ukJ3WDv3-aomYhVZ42RlvOlUMtrfNsZumYc80NB1bB60xGUip-T8Ty5hSseX4wjcA3M3qth-uUU9ri360tsW-Jm1sMvbXbR8HAzHLdCf5DVWLmnLOf=s0-d)
Select Server key in the popup.
![Server key](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_u5gCGbtIDmIvinpMMj3JINocKVbf5rdobboTj6FQdXQfk-L7HjpcSNnLeAaUqp6bMXvcsFw4LcIp1WyNd9JLdNnDUcH4tulxe6Azom9hkvyZHfVhQ=s0-d)
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](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_vwqWu6vfnGKP33HUN_B70eGMJnX2JGJ5j_uFUI3fB2yMM4s4N4qzyKYrdPM1qbPDu_gY2zXiBH7NdFAzaXHf1rFdvY51M5VjWrQxuToPPJ09_1XNwu=s0-d)
Now copy the API key in this screen.
![Copy key](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_sjcNacteL3UqYZyGJX6rOOIFxU0yBb0Y9q_21ahLE_LcE-cG3YPmqSBSrmPao1fUjIjAP5uLgq9h5NRv1m4v8FnYx5liIWBrdi0UXN2OP5A2DGjG1_=s0-d)
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('https://maps.googleapis.com/maps/api/timezone/json?location={0},{1}×tamp={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. http://www.indjango.com/google-api-to-get-timezone-from-lat-long-coordinates-in-python/