In one of my recent projects, I needed to get lat-long coordinates from address. I explored some APIs and found Google API a more reliable one. Just follow the steps given below to get it working.
Step 1 - Authentication
Sign up at Google developer console and create a project.
You can give a name of your choice and project ID will provided by Google.
Go to Enable an API section to enable APIs of your choice
In our case, we should enable Geocoding API and it provides 2,500 requests per day for free.
Now go to Credentials under APIs & auth. Click Create new Key.
Select Server key in the popup.
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.
Now copy the API key in this screen.
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 requests
address = "1600 Amphitheatre Parkway, Mountain View, CA"
api_key = "<api key copied from google>"
api_response = requests.get('https://maps.googleapis.com/maps/api/geocode/json?address={0}&key={1}'.format(address, api_key))
api_response_dict = api_response.json()
if api_response_dict['status'] == 'OK':
latitude = api_response_dict['results'][0]['geometry']['location']['lat']
longitude = api_response_dict['results'][0]['geometry']['location']['lng']
print 'Latitude:', latitude
print 'Longitude:', longitude
The output of this function will be
Latitude: 37.4224764
Longitude: -122.0842499
The full successful response from the API will be of the following format:
{u'results': [{u'address_components': [{u'long_name': u'1600',
u'short_name': u'1600',
u'types': [u'street_number']},
{u'long_name': u'Amphitheatre Parkway',
u'short_name': u'Amphitheatre Pkwy',
u'types': [u'route']},
{u'long_name': u'Mountain View',
u'short_name': u'Mountain View',
u'types': [u'locality', u'political']},
{u'long_name': u'Santa Clara County',
u'short_name': u'Santa Clara County',
u'types': [u'administrative_area_level_2', u'political']},
{u'long_name': u'California',
u'short_name': u'CA',
u'types': [u'administrative_area_level_1', u'political']},
{u'long_name': u'United States',
u'short_name': u'US',
u'types': [u'country', u'political']},
{u'long_name': u'94043',
u'short_name': u'94043',
u'types': [u'postal_code']}],
u'formatted_address': u'1600 Amphitheatre Parkway, Mountain View, CA 94043, USA',
u'geometry': {u'location': {u'lat': 37.4224764, u'lng': -122.0842499},
u'location_type': u'ROOFTOP',
u'viewport': {u'northeast': {u'lat': 37.4238253802915,
u'lng': -122.0829009197085},
u'southwest': {u'lat': 37.4211274197085, u'lng': -122.0855988802915}}},
u'place_id': u'ChIJ2eUgeAK6j4ARbn5u_wAGqWA',
u'types': [u'street_address']}],
u'status': u'OK'}
Now we have got latitude and longitude for the given address in few simple steps. Please feel free to post your comments.
More information can be read from Google geocoding docs.