Recently, I see a lot of sites that, when opened, give out a modal window with the intended city and the choice of another, if it is definitely wrong.

How to actually implement such a thing on your site? What is used for this js and api some kind of card?

3 answers 3

https://sypexgeo.net/

Here is another simple example.

$server = $_SERVER['REMOTE_ADDR']; $rrs = file_get_contents("http://api.sypexgeo.net/json/".$server); $obj = json_decode($rrs); $country = $obj->country->name_ru; 

    You can use Yandex API. In this example, two scripts are loaded: ymaps and jQuery for convenience. When ymaps loaded, a ymaps object of the same name appears in the global context. From it, you can get what you need. For example, a city: ymaps.geolocation.city . Or region: ymaps.geolocation.region . It's simple! You can also print the console.log(ymaps); object in the console.log(ymaps); and see all its properties and methods.

     <script src="http://yastatic.net/jquery/2.1.1/jquery.min.js"></script> <script src="http://api-maps.yandex.ru/2.0-stable/?load=package.standard&lang=ru-RU" type="text/javascript"></script> <script type="text/javascript"> window.onload = function () { jQuery("#user-city").text(ymaps.geolocation.city); jQuery("#user-region").text(ymaps.geolocation.region); jQuery("#user-country").text(ymaps.geolocation.country); } </script> <div id="user-city"></div> <div id="user-region"></div> <div id="user- country"></div> 

      By server-side IP, you can determine the location of your provider. To more accurately determine your location, you can use the Geolocation API . In this case, the browser will request confirmation.