开发者

trying to get user location through IP

I am trying to locate a user through his/her IP address and then开发者_JS百科 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.


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/


You can use following, but I am not sure if this would work in all browsers.

if (navigator) {
     if (navigator.geolocation) {
         navigator.geolocation.getCurrentPosition(function (position) {
             do_something(position.coords.latitude, position.coords.longitude);
         });
     }
 }


Locating a user would depend on the device you are targeting and granularity/accuracy with which you want to display his location on the map.

The ip address is more suited for laptops and other non-GPS devices. Secondly as you'd know, ip address will only pin point the user's gateway. In my case the gateway is about 8 miles from where I reside so you can imagine the accuracy is only suited for a certain class of apps such analysis tools like omniture or mixpanel.

On the other hand if you are developing your app for a mobile device you can start off reading about it in the question posed here. Using gps/agps will give you accuracy of within a few meters. This is more suited for LBS applications like gowalla or foursquare. HTML5 makes it easy for you to implement the latter especially if you are targeting iOS and Android devices.

Some off the cuff code in python is given below. Details of gmaps api v3 can be found here.

Server implementation in Python to record location and generate markers

class RecordLocation(webapp.RequestHandler):
  def post(self):
    session=SessionManager(self)
        if session.is_set():  marker=Markers(lat=self.request.get('lat'),lon=self.request.get('lon'),user_id=self.request.get('user'))
        marker.put()
        self.response.out.write('<html><body>')
        self.response.out.write(" Location Updated<br/>")
        self.response.out.write('</body></html>')

    class GenerateMarkers(webapp.RequestHandler):
        def get(self):
            session=SessionManager(self)
            if session.is_set():
                markers=db.GqlQuery("SELECT * FROM Markers")
                doc='<?xml version="1.0"?>'
                doc+='<markers>'
                for marker in markers:
                    doc+='<marker '
                    doc+='lat="'+marker.lat+'" '
                    doc+='lon="'+marker.lon+'" '
                    doc+='type="restaurant" '
                    doc+='/>'
                    doc+='</markers>'
                self.response.out.write(doc)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜