Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python code for reverse Geo-coding using Google API

I am getting the geo-data in a json file (geo.json) which has the following structure

 {"userId":"Geo-data","data":{"mocked":false,"timestamp":1548173963281,"coords":{"speed":0,"heading":0,"accuracy":20.20400047302246,"longitude":88.4048656,"altitude":0,"latitude":22.5757344}}}

All I want is to print the place details corresponding to above data and if possible to show it on MAP also.I have tried the following code with geopy

from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.reverse("22.5757344, 88.4048656")
print(location.address)
print((location.latitude, location.longitude))

But the location I am getting is not very accurate. Whereas the same coordinates give good results in https://www.latlong.net/Show-Latitude-Longitude.html I also have a Google API key. However, what ever references I found so far are almost like a project themselves and a overkill for a beginner like me. The geopy code was fine but the location accuracy is very poor. Please help.

P.S I have tried geocoder also as

import geocoder
g = geocoder.google([45.15, -75.14], method='reverse')
print(g.city)
print(g.state)
print(g.state_long)
print(g.country)
print(g.country_long)

However it is printing 'None' in all the cases.

like image 506
Bukaida Avatar asked Oct 27 '25 17:10

Bukaida


1 Answers

You could consider to switch from OpenStreetMap Nominatim provider to Google Geocoding. Then, the following example seems returns the address you expect it to:

from geopy.geocoders import GoogleV3

geolocator = GoogleV3(api_key=google_key)
locations = geolocator.reverse("22.5757344, 88.4048656")
if locations:
    print(locations[0].address)  # select first location

Result

R-1, GA Block, Sector III, Salt Lake City, Kolkata, West Bengal 700106, India
like image 74
Vadim Gremyachev Avatar answered Oct 30 '25 12:10

Vadim Gremyachev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!