尝试通过IP获取用户位置

发布于 2024-09-30 08:49:22 字数 94 浏览 2 评论 0原文

我试图通过他/她的 IP 地址找到用户,然后在谷歌地图中显示他们。为此目的最好的方法是什么?或者有没有其他方法可以获取用户纬度/经度并在谷歌地图中显示它们而不是 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧人哭 2024-10-07 08:49:22

你的问题实际上是2个问题。第一部分是从 IP 地址获取 lat、lng。 AFAIK,谷歌地图 API 不提供基于 IP 的地理定位。因此,要在谷歌地图上显示 IP 地址,您必须首先获取地理位置(可以是地址,也可以是 lat、lng)。网络上有各种付费和免费的 API 允许您执行此操作,并且 django 本身附带了一个用于此目的的实用程序。
文档位于此处。您的用例将实现为:

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

address = g.city(user_ip)

注意:此实用程序或许多免费的基于 IP 的地理定位 API 之一可能不是非常准确。

至于第二部分,要在谷歌地图上显示此位置,您可以查看此应用程序:

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

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/

深海蓝天 2024-10-07 08:49:22

您可以使用以下内容,但我不确定这是否适用于所有浏览器。

if(导航器){
     if (navigator.geolocation) {
         navigator.geolocation.getCurrentPosition(函数(位置){
             do_something(position.coords.latitude,position.coords.longitude);
         });
     }
 }

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);
         });
     }
 }
韶华倾负 2024-10-07 08:49:22

定位用户将取决于您所定位的设备以及您想要在地图上显示其位置的粒度/准确性。

IP 地址更适合笔记本电脑和其他非 GPS 设备。其次,如您所知,IP 地址只能确定用户的网关。就我而言,网关距离我居住的地方大约 8 英里,因此您可以想象,其准确性仅适合某一类应用程序,例如omnture 或 mixpanel 等分析工具。

另一方面,如果您正在为移动设备开发应用程序,您可以开始在提出的问题中阅读相关内容 此处。使用 gps/agps 的精度可以达到几米之内。这更适合 Gowalla 或 foursquare 等 LBS 应用程序。 HTML5 使您可以轻松实现后者,特别是如果您的目标是 iOS 和 Android 设备。

下面给出了 python 中的一些即兴代码。您可以在此处找到 gmaps api v3 的详细信息。

Python 中的服务器实现,用于记录位置并生成标记

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文