Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trying to get user location through IP

I am trying to locate a user through his/her IP address and then show them in a google map. what would be the best approach for this purpose. Or is there any other way to get the user lat/long and show em in a google map instead of IP address.

like image 624
MajorGeek Avatar asked Nov 03 '10 19:11

MajorGeek


1 Answers

Your question is actually 2 questions. The first part is getting lat, lng from an ip address. AFAIK, google maps api doesnt provide for IP based geolocation. Therefore to show the IP address on a google map, you must first get the geographic location (either as an address, or as lat, lng). There are various paid and unpaid APIs on the web that allow you to do this, and django itself comes with a utility for this. The docs are here. Your use case would be implemented as:

from django.contrib.gis.utils import GeoIP
g = GeoIP()
lat,lng = g.lat_lon(user_ip)

or

address = g.city(user_ip)

NOTE: This utility, or one of the many free IP based geolocation APIs might not be VERY accurate.

As for the second part, to show this location on a google map, you could check out this application:

http://code.google.com/p/django-googlemap/

like image 175
zsquare Avatar answered Nov 07 '22 05:11

zsquare