Cómo obtener latitud y longitud con python

Estoy tratando de recuperar la longitud y la latitud de una dirección física, a través de la siguiente secuencia de comandos. Pero estoy recibiendo el error. Ya he instalado googlemaps. amablemente responder gracias de antemano

#!/usr/bin/env python import urllib,urllib2 """This Programs Fetch The Address""" from googlemaps import GoogleMaps address='Mahatma Gandhi Rd, Shivaji Nagar, Bangalore, KA 560001' add=GoogleMaps().address_to_latlng(address) print add 

Salida:

 Traceback (most recent call last): File "Fetching.py", line 12, in  add=GoogleMaps().address_to_latlng(address) File "/usr/local/lib/python2.7/dist-packages/googlemaps.py", line 310, in address_to_latlng return tuple(self.geocode(address)['Placemark'][0]['Point']['coordinates'][1::-1]) File "/usr/local/lib/python2.7/dist-packages/googlemaps.py", line 259, in geocode url, response = fetch_json(self._GEOCODE_QUERY_URL, params=params) File "/usr/local/lib/python2.7/dist-packages/googlemaps.py", line 50, in fetch_json response = urllib2.urlopen(request) File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen return _opener.open(url, data, timeout) File "/usr/lib/python2.7/urllib2.py", line 407, in open response = meth(req, response) File "/usr/lib/python2.7/urllib2.py", line 520, in http_response 'http', request, response, code, msg, hdrs) File "/usr/lib/python2.7/urllib2.py", line 445, in error return self._call_chain(*args) File "/usr/lib/python2.7/urllib2.py", line 379, in _call_chain result = func(*args) File "/usr/lib/python2.7/urllib2.py", line 528, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 403: Forbidden 

El paquete googlemaps que está utilizando no es oficial y no utiliza la API de google maps v3, que es la última de google.

Puede usar la API de REST de geoencoding de google para obtener las coordenadas de la dirección. Aquí hay un ejemplo.

 import requests response = requests.get('https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA') resp_json_payload = response.json() print(resp_json_payload['results'][0]['geometry']['location']) 

La forma más sencilla de obtener Latitude y Longitude utilizando Google Api, Python y Django.

 # Simplest way to get the lat, long of any address. # Using Python requests and the Google Maps Geocoding API. import requests GOOGLE_MAPS_API_URL = 'http://maps.googleapis.com/maps/api/geocode/json' params = { 'address': 'oshiwara industerial center goregaon west mumbai', 'sensor': 'false', 'region': 'india' } # Do the request and get the response data req = requests.get(GOOGLE_MAPS_API_URL, params=params) res = req.json() # Use the first result result = res['results'][0] geodata = dict() geodata['lat'] = result['geometry']['location']['lat'] geodata['lng'] = result['geometry']['location']['lng'] geodata['address'] = result['formatted_address'] print('{address}. (lat, lng) = ({lat}, {lng})'.format(**geodata)) # Result => Link Rd, Best Nagar, Goregaon West, Mumbai, Maharashtra 400104, India. (lat, lng) = (19.1528967, 72.8371262) 

Intente este código: –

 from geopy.geocoders import Nominatim geolocator = Nominatim() city ="London" country ="Uk" loc = geolocator.geocode(city+','+ country) print("latitude is :-" ,loc.latitude,"\nlongtitude is:-" ,loc.longitude)