从ip地址获取用户纬度和经度的最简单方法是什么 [英] What's the simplest way to get a user's latitude and longitude from an ip address

查看:36
本文介绍了从ip地址获取用户纬度和经度的最简单方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我从事的大多数网络项目中,我总是需要用户的位置.我使用过 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.

推荐答案

我目前使用的实现我已经在其他几个问题中看到过,但我没有看到它作为选择的答案.

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

我已在我的博客 thenullreference.com,但简而言之:

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

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

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.

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

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

*注意其中一些可以为空

*Note some of these can be null

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

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?

我不知道此解决方案的覆盖范围/准确性,但我认为 Google 会对其进行更新,并且可能相当不错.

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

这篇关于从ip地址获取用户纬度和经度的最简单方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆