从 IP 地址获取用户的纬度和经度的最简单方法是什么

发布于 2024-08-07 18:20:21 字数 268 浏览 1 评论 0 原文

在我从事的大多数网络项目中,我总是需要用户的位置。我使用过 Maxmind 的 GeoIp,但它涉及您导入他们的数据集并且需要不断更新。还有其他免费和付费服务,但我只想要一些简单的东西,我可以在几秒钟内添加到任何网站。

所以这有点像诱饵,因为我有一个简单的解决方案,详细信息如下,但我想看看是否有其他人使用这种技术,是否有更好的解决方案,或者是否存在一些不可预见的陷阱。

I always end up needing the user's location on most web projects that I work on. I've used Maxmind's GeoIp, but it involves you importing their dataset and it needs constant updating. There are also other free and paid services, but I just wanted something simple that I could add to any site in a matter of seconds.

So this is sort of baited because I have a simple solution, detailed below, but I wanted to see if anyone else uses this technique and if there are any better solutions or if there are some unforeseen pitfalls.

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

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

发布评论

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

评论(3

翻了热茶 2024-08-14 18:20:21

我目前使用的实现已经在其他几个问题中看到过,但我还没有将其视为选定的答案。

我在我的博客中详细介绍了这一点 thenullreference.com,但简而言之:


基本上,您需要做的就是加载 Google 的 API 加载器脚本:

<script type="text/javascript" src="http(s)://www.google.com/jsapi"></script>

然后您就可以访问多个属性,为您提供详细的位置信息。

您要查看的对象是 google.loader.ClientLocation

  • ClientLocation.latitude
  • ClientLocation.longitude
  • ClientLocation.address.city
  • ClientLocation.address.country
  • ClientLocation.address.country_code - ISO 3166-1 国家/地区代码
  • ClientLocation。地址.区域 - 美国的国家/地区特定区域,这是州

*注意,其中一些可能为 null

有关此 API 的更多信息,请查看 这里

还有其他人使用这个吗?有人使用过同样简单且更好的东西吗?这种方法有问题吗?

我不知道这个解决方案的覆盖范围/准确性,但我认为谷歌会不断更新它并且可能相当不错。

The implementation I currently use I've seen in a couple other questions, but I haven't seen it as the selected answer.

I've detailed this in my blog thenullreference.com, but here it is in short:


Essentially all you need to do is load Google's API loader script:

<script type="text/javascript" src="http(s)://www.google.com/jsapi"></script>

Then you have access to several properties that give you detailed location info.

The object you want to look at is google.loader.ClientLocation

  • ClientLocation.latitude
  • ClientLocation.longitude
  • ClientLocation.address.city
  • ClientLocation.address.country
  • ClientLocation.address.country_code - ISO 3166-1 country code
  • ClientLocation.address.region - country specific region in US this is state

*Note some of these can be null

For more info on this API check out here

Does anyone else use this? Does anyone have something that they use that is as simple and better? Are there issues with this approach?

I'm unaware of the coverage/accuracy of this solution, but I would think that Google keeps it updated and is probably pretty good.

难如初 2024-08-14 18:20:21

我使用过 wipmania 免费 JSONP 服务,我不太确定它的准确性,但它非常简单使用:

// an example using jQuery
$.getJSON('http://api.wipmania.com/jsonp?callback=?', function (data) { 
  alert('Latitude: ' + data.latitude + 
        '\nLongitude: ' + data.longitude + 
        '\nCountry: ' + data.address.country); 
});

检查此处运行的代码片段。

I've used the wipmania free JSONP service, I'm not really sure about its accuracy but it's really simple to use:

// an example using jQuery
$.getJSON('http://api.wipmania.com/jsonp?callback=?', function (data) { 
  alert('Latitude: ' + data.latitude + 
        '\nLongitude: ' + data.longitude + 
        '\nCountry: ' + data.address.country); 
});

Check the snippet running here.

会傲 2024-08-14 18:20:21

我使用 MaxMind GeoIP City/ISP/Organization(Web 服务),50,000 个查询的成本为 20 美元,并且始终更新(请参阅 http://www.maxmind.com/app/web_services#city)。我喜欢这样一个事实:我不需要担心数据库更新,因为这一切都在服务器上完成。

我使用 PHP 发送客户端 IP 地址,作为回报,我得到纬度、经度以及其他有用的信息,如区域代码、城市、大都市代码、区号、国家/地区、ISP 和组织名称(如果已知)。

I use the MaxMind GeoIP City/ISP/Organization (Web Service), this cost $20 for 50,000 queries and is always updated (see http://www.maxmind.com/app/web_services#city). I like the fact that I don't need to worry about database updates since it is all done on there server.

I'm using PHP to send the client IP address and in return I get the latitude, longitude as well as other useful like the region code, city, metropolitan code, area code, country, ISP, and organization name if known.

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