如何获取用户的地理位置? [英] How to get user's geolocation?

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

问题描述

在很多网站上,我看到了我现在的城市(例如你好,柏林)。他们如何做到这一点?什么都需要呢?
我猜主要部分是在这里的JavaScript,但什么我需要实现这样的东西到我自己的应用程序? (或者是否有一些Rails的宝石?)

另外,我还想问一件事 - 我在状态列表中感兴趣(通常在选择框中),其中用户选择他的状态(比如说德国),根据状态值在另一选择中显示德国所有地区,并在选择地区后显示所选地区的各个城市。



可以在任何地方获得这个庞大的州/城市/地区数据库吗?在我们的应用程序中有类似的东西会很有趣,但我不知道,那些列表在哪里得到...

你需要一个支持地理定位API的浏览器来获取用户的位置(然而,你需要用户的同意(例如这里)这样做(大多数新的浏览器都支持该功能,包括IE9 +和大多数移动操作系统的浏览器,包括Windows Phone 7.5 +)。



所有你需要做的就是使用JavaScript来获取位置:

$ $ p $ $ $ $ $ c $ if(window.navigator.geolocation){
var failure,success;
success = function(position){
console.log(position);
};
failure = function(message){
alert('Can not retrieve location!');
};
navigator.geolocation.getCurrentPosition(成功,失败,{
maximumAge:Infinity,
timeout:5000
});
}

位置对象将保存用户位置的经度和纬度(但在桌面浏览器上人口较少的地区,由于它们没有内置GPS设备,因此这可能非常不准确)。进一步解释:在莱比锡,在台式电脑上获得约300米的精确度 - 我的手机GPS设备的准确度约为30米。



然后,您可以继续使用 Google Maps API 中的坐标(请参阅< href =https://developers.google.com/maps/documentation/geocoding/#ReverseGeocoding =nofollow noreferrer>这里用于反向地理编码)来查找用户的位置。如果您愿意,可以使用 Rails几个宝石。我从来没有觉得有必要使用它们,但有些人似乎喜欢它们。



至于国家/城市列表,我们使用可从 Geonames 曾经在一个项目中,但我们需要首先将它转换为我们的需求。


On many sites I saw printed out my current city where I am (eg "Hello to Berlin."). How they do that? What everything is needed for that? I guess the main part is here javascript, but what everything I need for implementing something like this to my own app? (or is there some gem for Rails?)

Also, I would like to ask for one thing yet - I am interesting in the list of states (usually in select box), where user select his state (let's say Germany), according to the state value are in another select displayed all regions in Germany and after choosing a region are displayed respective cities in the selected region.

Is possible anywhere to obtain this huge database of states/cities/regions? Would be interesting to have something similar in our app, but I don't know, where those lists get...

解决方案

You need a browser which supports the geolocation api to obtain the location of the user (however, you need the user's consent (an example here) to do so (most newer browsers support that feature, including IE9+ and most mobile OS'es browsers, including Windows Phone 7.5+).

all you have to do then is use JavaScript to obtain the location:

if (window.navigator.geolocation) {
    var failure, success;
    success = function(position) {
      console.log(position);
    };
    failure = function(message) {
      alert('Cannot retrieve location!');
    };
    navigator.geolocation.getCurrentPosition(success, failure, {
      maximumAge: Infinity,
      timeout: 5000
    });
}

The positionobject will hold latitude and longitude of the user's position (however this can be highly inaccurate in less densely populated areas on desktop browsers, as they do not have a GPS device built in). To explain further: Here in Leipzig in get an accuracy of about 300 meters on a desktop computer - i get an accuracy of about 30 meters with my cell phone's GPS device.

You can then go on and use the coordinates with the Google Maps API (see here for reverse geocoding) to lookup the location of the user. There are a few gems for Rails, if you want. I never felt the need to use them, but some people seem to like them.

As for a list of countries/cities, we used the data obtainable from Geonames once in a project, but we needed to convert it for our needs first.

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

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