How to find out a user's region using Geolocation HTML5

How do I find out the region of the user who visited the site?

Author: vasily_dumov, 2016-07-14

3 answers

Something similar can be used (determining the location by coordinates):

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<p>Страна: <span id="country"></span></p>
<p>Край(обл.): <span id="state"></span></p>
<p>Город: <span id="city"></span></p>
<p>Адрес: <span id="address"></span></p>
<p>Широта: <span id="latitude"></span></p>
<p>Долгота: <span id="longitude"></span></p>

JS

function displayError(error) {
  var errors = {
    1: 'Нет прав доступа',
    2: 'Местоположение невозможно определить',
    3: 'Таймаут соединения'
  };
  alert("Ошибка: " + errors[error.code]);
}

function displayPosition(position) {
  var GEOCODING = 'https://maps.googleapis.com/maps/api/geocode/json?latlng=' + position.coords.latitude + '%2C' + position.coords.longitude + '&language=ru';

  $.getJSON(GEOCODING).done(function(location) {

    $('#address').html(location.results[0].formatted_address);
    $('#latitude').html(position.coords.latitude);
    $('#longitude').html(position.coords.longitude);

    // в location.results[0] содержится больше всего информации об адресе
    for (var i = 0; i < location.results[0].address_components.length; i++) {
       switch(location.results[0].address_components[i].types[0]) {
         case 'locality':
           $('#city').html(location.results[0].address_components[i].long_name);
           break;
         case 'administrative_area_level_1':
           $('#state').html(location.results[0].address_components[i].long_name);
           break;
         case 'country':
           $('#country').html(location.results[0].address_components[i].long_name);
           break;
       }
    }
  })
}

if (navigator.geolocation) {
  var timeoutVal = 10 * 1000 * 1000;
  navigator.geolocation.getCurrentPosition(
    displayPosition,
    displayError, {
      enableHighAccuracy: true,
      timeout: timeoutVal,
      maximumAge: 0
    }
  );
} else {
  alert("Geolocation не поддерживается данным браузером");
}

Working example: https://jsfiddle.net/tty37u7m/6/ (this example could not be imported into the code here, geolocation does not respond)

 6
Author: Visman, 2017-08-01 12:00:57

As long as there are NAT and "gray" networks, you can forget about geolocation.

The fact is that due to the lack of public ("white", everywhere visible) IPv4 addresses, the Internet provider assigns them only to a certain number of its devices, the so-called "NAT gateways". Subscribers, just like internal routers, are assigned local ("gray") addresses that are invisible outside the gateway; these addresses are repeated from provider to provider.

The base is GeoIP, which stores in itself geo-referenced subnets, contains only public subnets. As a result, geolocation determines the position of the provider gateway to the "external" Internet, not the user. Given that there are usually several gateways, each subsequent connection can be "routed" through different, probably geographically distributed, gateways.

For example: I live in Tolyatti, but geolocation determines that I live in Samara, then in Moscow.

 2
Author: Arhadthedev, 2016-09-04 08:31:02

Alternatively, you can use the ready-made service https://location.ekolesnyk.com/ make api request get the answer country city coordinates, example

 $url = 'https://location.ekolesnyk.com/api/v1/country/?apiKey={apiKey}&ip=www.google.com';
    $response = file_get_contents($url);
    $json = json_decode($response, true); 

  if((isset($json['error']))){
  return $json['error']; //message error
  }

  return $json;
 0
Author: Евгений Колесник, 2018-08-30 21:04:10