How to find out the region of the user who went to the site?

    3 answers 3

    Something similar can be used ( positioning 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, geolocation does not respond)

      As long as NAT and “gray” networks exist, you can forget about geolocation.

      The fact is that because of the lack of public (“white”, everywhere visible) IPv4 addresses, the Internet provider assigns them only to a certain number of their devices, the so-called “NAT gateways”. The subscribers, as well as the internal routers, are assigned local (“gray”) addresses that are invisible outside the gateway; These addresses are repeated from provider to provider.

      The GeoIP base, which stores the geographic location of subnets, contains only public subnets. As a result, geolocation determines the position of not the user, but the provider gateway to the “external” Internet . Given that there are usually several gateways, each subsequent connection can be "forwarded" through different, probably geographically distributed, gateways.

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

        as an option you can use the ready service https://location.ekolesnyk.com/ do an api request get an answer country country 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;