M
M
M4xwell2020-05-30 22:29:48
API
M4xwell, 2020-05-30 22:29:48

How to correctly determine the user's city using the Yandex Maps API?

There is this code:

сначала подключаю api
 <script src="https://api-maps.yandex.ru/2.1/?lang=ru_RU&amp;apikey=тут_мой_ключ" type="text/javascript"></script>   

<script>
ymaps.ready(function() {
  ymaps.geolocation.get({
   provider: 'yandex',
   //autoReverseGeocode: true

  }).then(function (result) {
    var g = result.geoObjects.get(0);
    $('#city').html(g.getLocalities()[0]);
  });
});
</script>


well, insert the result into the div with id city .
The task is to correctly determine the user's city and display it on the site.
What is the glitch? From a home PC, it determines correctly (within the framework of the router). When checking through a mobile phone (3g / LTE) - it determines the city incorrectly at all, Moscow writes, although I am more than 2000 km from it. Is there a way to correctly define this whole thing?
glitch - does not work at all when using provider: browser (I would also like to understand why)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2020-05-31
@freeExec

From the mobile you need GPS, well, if the user allows you, of course. Or ask him to explicitly specify the city. The IP address that is given to the mobile in the databases is tied to the operator's head office. Here he is in Moscow, and you can even go to Alaska, this will not cause a miracle in the database and the position of the address will not change.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question