1

I am working on a Django project where I need to get the longitude and latitude of the current location of the user. I am using the geopy for this:

from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.geocode("175 5th Avenue NYC")
print((location.latitude, location.longitude))

But in above case, I am adding address manually, but as per my requirement I want to get address of the user automatically.

halfer
  • 19,824
  • 17
  • 99
  • 186
Amrinder Singh
  • 5,300
  • 12
  • 46
  • 88
  • Ask user to give you location, if the user don't want to give location access respect the decision and move on. – Aamir Rind Dec 03 '15 at 11:54
  • yeah exactly, but i am unable to add this functionality where i can ask user to give its location, can you please help me – Amrinder Singh Dec 03 '15 at 11:55
  • 1
    I assume this is running in a browser on the end user device - then you need to use the HTML5 API for geolocation and send it back to your server. The user can allow or deny such access. – henrikstroem Dec 03 '15 at 11:56
  • @Insomania you could try google maps API check this http://stackoverflow.com/questions/13478646/google-map-api-get-latitude-and-longitude-from-autocomplete-without-map – Aamir Rind Dec 03 '15 at 12:03

2 Answers2

1

Hostip.info is an open-source project with the goal to build/maintain a database mapping IP addresses to cities. Their about page explains the data sources relied on to populate this database.

Using HostIP, there are two ways to get location data from an IP address:

They also have a well-designed and easy-to-use RESTFUL API: just pass in your ip address after the ip= in the GET request string):

 import urllib

response = urllib.urlopen('http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true').read()

print(response);
Aman Gupta
  • 1,764
  • 4
  • 30
  • 58
1

You can try HTML5 geolocation or you can use Google Places providing an autocomplete address field which will return you details about the address given.

You can use geocomplete plugin to get the user address which uses Google Maps API and providing ease of access to have autocomplete and other features. Check the example here.

Aamir Rind
  • 38,793
  • 23
  • 126
  • 164