How to get user's geolocation?

2020-02-29 02:52发布

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...

标签: geolocation
7条回答
看我几分像从前
2楼-- · 2020-02-29 03:30

Internet Service Providers buy up big chunks of IP addresses, so what you're most likely seeing is a backtrace your IP to a known ISP. They have a database with ISP's and their location in the world, so they can try to see where you're from. You could try to use a site like http://www.ipaddresslocation.org/ to do your work. If you look around, there is bound to be a site that lets you enter an IP and get a location, so you just send a POST request to that site with your visitor's IP and scrape the location from the response.

Alternatively you could try to look for an ISP database that has location and what chunks of the IP range they have been allocated. You could probably find one for money, but a free one might be harder to find. Alternatively, check out this free database http://www.maxmind.com/app/geolite

查看更多
【Aperson】
3楼-- · 2020-02-29 03:37

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*/})
查看更多
劳资没心,怎么记你
4楼-- · 2020-02-29 03:38

Here is an another api to find out the location in PHP,

http://ipinfodb.com/ip_location_api.php

查看更多
家丑人穷心不美
5楼-- · 2020-02-29 03:42

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");
        }
    );         
查看更多
成全新的幸福
6楼-- · 2020-02-29 03:46

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

查看更多
Viruses.
7楼-- · 2020-02-29 03:50

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

查看更多
登录 后发表回答