How to get Address from Latitude & Longitude in Django GeoIP?

后端 未结 4 1228
遇见更好的自我
遇见更好的自我 2021-02-06 08:39

I cannot see anything in their API to do this: https://docs.djangoproject.com/en/dev/ref/contrib/gis/geoip/#geoip-api

Or should I just use Google API for Reverse Geocodi

4条回答
  •  时光取名叫无心
    2021-02-06 08:44

    @rawsix answer seems smart for a django user. Note however that the location returned by geolocator.reverse(query) is a list and not a Location object; so attempting to retrieve attribute address from it would result in an error.

    Usually, the first item in that list has the closest address information. so u can simply do:

     location = location[0]
     address = location.address
    

    Additionally, instead of passing a string latitude and longitude to the reverse method, you can use a tuple and it must be the latitude before the longitude. You can do:

     from geopy.geocoders import GoogleV3()
     geocoder = GoogleV3()
     location_list = geocoder.reverse((latitude, longitude))
     location = location_list[0]
     address = location.address
    

提交回复
热议问题