如何使用地理位置获取访问者的位置(即国家/地区)? [英] How to get visitor's location (i.e. country) using geolocation?

查看:165
本文介绍了如何使用地理位置获取访问者的位置(即国家/地区)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图扩展本地地理定位功能
$ b

  if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
});
}

以便我可以使用访问者的国家名称(也许返回一个信息丰富的数组) 。

到目前为止,我所能找到的都是显示谷歌地图界面的函数,但实际上并没有给出我想要的东西,除了这个图书馆运作良好在这个例子中,但由于某种原因在我的电脑上无法正常工作。我不知道为什么那里出了错。



无论如何,你知道我怎样才能简单地返回包含国家,城市等信息的数组,从纬度和经度值?

解决方案

如果您只需要他们的国家,则不需要找到用户。您可以在任何IP到位服务中查找其IP地址(例如 maxmind )。这在大多数情况下都是准确的。



如果您确实需要获取他们的位置,您可以使用该方法获取他们的纬度/经度,然后查询 Google的雅虎的反向地理编码服务。


I'm trying to extend the native geolocation function

if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
    });
}

so that I can use the visitor's country name (perhaps return an informative array).

So far all I've been able to find are functions that display a google maps interface but none actually gave what I want, except for this library which worked well in this example but for some reason didn't work on my computer. I'm not sure why that went wrong there.

Anyways, do you know how I can simply return an array containing information like country, city, etc. from latitude and longitude values?

解决方案

You don't need to locate the user if you only need their country. You can look their IP address up in any IP-to-location service (like maxmind). This will be accurate most of the time.

If you really need to get their location, you can get their lat/lng with that method, then query Google's or Yahoo's reverse geocoding service.

这篇关于如何使用地理位置获取访问者的位置(即国家/地区)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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