Python query the latitude and longitude of a city Google Maps

Python query the latitude and longitude of a city Google Maps

python

Note: Please use 4 spaces inside the python code for child lines.

#!/usr/bin/env python

import argparse
import os
import urllib
import requests
ERROR_STRING = ‘<error>’

def find_lat_long(city):
“”” Find geographic coordinates “””
# Encode query string into Google maps URL
#url = ‘https://www.google.com/maps/place/’ + urllib.quote(city)
response = requests.get(‘https://maps.googleapis.com/maps/api/geocode/json?address=’ + urllib.quote(city))
resp_json_payload = response.json()
# Get XML location from Google maps
print(resp_json_payload[‘results’][0][‘geometry’][‘location’])
# Strip lat/long coordinates from XML
if __name__ == ‘__main__’:
parser = argparse.ArgumentParser(description=’City Geocode Search’)
parser.add_argument(‘–city’, action=”store”, dest=”city”, required=True)
given_args = parser.parse_args()
# print “Finding geographic coordinates of %s” %given_args.city
find_lat_long(given_args.city)

root@ubuntu:~/python# python 6_2_googlemap.py –city “New York”
{u’lat’: 40.7127837, u’lng’: -74.0059413}
root@ubuntu:~/python#

Leave a reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>