How to get custom geolocation?

On many sites, I saw a printout of my current city, where I am (for example, "Hello to Berlin"). How do they do it? What is needed for this? I assume the main part is javascript here, but what do I need to implement something similar in my own application? (or is there some kind of stone for Rails?)

In addition, I would like to ask one more thing - I’m interested in the list of states (usually in the selection field), where the user selects his state (say, in Germany), according to the state value in another select are displayed in all regions of Germany, and After selecting a region, the corresponding cities in the selected region are displayed.

Is there any way to get this huge database from states / cities / regions? It would be interesting to have something like this in our application, but I don't know where these lists get ...

+5
source share
7 answers

You need a browser that supports api geolocation to get the user's location (however you need user consent (example here ) for this (most new browsers support this feature, including IE9 + and most mobile OS browsers, including Windows Phone 7.5 +).

all you have to do is use javascript to get 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
    });
}

position ( , GPS-). : , , 300 - 30 GPS .

API Google (. ) . , Rails. , , , .

/, , Geonames , .

+3

getCurrentPosition(), , , GPS, . JavaScript, getCurrentPosition(), , , .

:

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

- https://github.com/gwilson/getAccurateCurrentPosition

+1

Correc:

navigator.geolocation.getCurrentPosition(successCallBack, failureCallBack);

:

    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");
        }
    );         
+1

- IP-, , , , , - IP- -. ISP , , . , http://www.ipaddresslocation.org/, . , , IP- , POST- IP- .

ISP, , IP. , , . , http://www.maxmind.com/app/geolite

0

geoip.maxmind.com , 100%. HTTP-.

0

ES6 promises,

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');
    }
  })
}

:

getPositionPromised()
  .then(position => {/*do something with position*/})
  .catch(() => {/*something went wrong*/})
0

All Articles