5

在许多网站上,我看到打印出我当前所在的城市(例如“Hello to Berlin.”)。他们是怎么做到的?为此需要什么?我想主要部分是这里的 javascript,但是在我自己的应用程序中实现这样的东西需要什么?(或者 Rails 有什么宝石吗?)

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

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

4

7 回答 7

3

您需要一个支持 geolocation api 的浏览器来获取用户的位置(但是,您需要用户的同意(此处的示例)才能这样做(大多数较新的浏览器支持该功能,包括 IE9+ 和大多数移动操作系统的浏览器,包括 Windows Phone 7.5+)。

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

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.

于 2012-08-08T12:18:33.400 回答
1

I've found getCurrentPosition() to often be inaccurate since it doesn't spend a lot of time waiting on the GPS to acquire a good accuracy. I wrote a small piece of JavaScript that mimics getCurrentPosition() but actually uses watch position and monitors the results coming back until they are better accuracy.

Here's how it looks:

navigator.geolocation.getAccurateCurrentPosition(onSuccess, onError, {desiredAccuracy:20, maxWait:15000});

Code is here - https://github.com/gwilson/getAccurateCurrentPosition

于 2012-08-12T17:06:40.617 回答
1

Correc syntax would be :

navigator.geolocation.getCurrentPosition(successCallBack, failureCallBack);

Use :

    navigator.geolocation.getCurrentPosition(
        function(position){
            var latitude  = position.coords.latitude;
            var longitude = position.coords.longitude;
            console.log("Latitude : "+latitude+" Longitude : "+longitude);
        },
        function(){
            alert("Geo Location not supported");
        }
    );         
于 2013-10-21T09:08:04.810 回答
1

If you prefer to use ES6 and promises here is another version

function getPositionPromised() {
  function successCb(cb) {
    return position => cb(position);
  }

  function errorCb(cb) {
    return () => cb('Could not retrieve geolocation');
  }

  return new Promise((resolve, reject) => {
    if (window.navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(successCb(resolve), errorCb(reject));
    } else {
      return reject('No geolocation support');
    }
  })
}

And you can use it like this:

getPositionPromised()
  .then(position => {/*do something with position*/})
  .catch(() => {/*something went wrong*/})
于 2016-08-31T22:36:31.490 回答
0

互联网服务提供商购买了大量的 IP 地址,因此您最有可能看到的是您的 IP 回溯到已知的 ISP。他们有一个包含 ISP 及其在世界上的位置的数据库,因此他们可以尝试查看您来自哪里。您可以尝试使用http://www.ipaddresslocation.org/之类的网站来完成您的工作。如果您环顾四周,肯定会有一个站点允许您输入 IP 并获取位置,因此您只需使用访问者的 IP 向该站点发送 POST 请求并从响应中获取位置。

或者,您可以尝试查找具有位置以及已分配 IP 范围的哪些块的 ISP 数据库。你可能会为了钱找到一个,但一个免费的可能更难找到。 或者,查看这个免费数据库http://www.maxmind.com/app/geolite

于 2012-08-08T12:11:47.413 回答
0

这是另一个在 PHP 中查找位置的 api,

http://ipinfodb.com/ip_location_api.php

于 2012-08-08T12:17:23.320 回答
0

I have been using geoip.maxmind.com for quite a while and it works 100%. It can be accessed via HTTP requests.

于 2015-03-27T18:57:00.887 回答