How do I get the name of a city from the width and longitude?

Got the latitude and longitude (navigator.geolocation.getCurrentPosition). Can I get the name of the city?

if(navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(function(position) {
              var latitude = position.coords.latitude;
              var longitude = position.coords.longitude;                 
        alert(latitude+' '+longitude);
  });

} else {
    alert("Geolocation API не поддерживается в вашем браузере");
}

And what does it look like anyway?
1) found out the width and longitude,
2) then I insert this data into a request to the yandex api
3) got the city (just how to get it out?)

By IP is not an option.

Author: evans, 2017-05-29

1 answers

I solved your problem using the yandex maps api: Here is an example and after it I will tell you in detail what and how it happens.

<html>
    <head>
        <script charset="utf-8" src="https://api-maps.yandex.ru/1.1/index.xml" type="text/javascript"></script>
    </head>
    <body>
        <script>            
        // Создание обработчика для события window.onLoad
        YMaps.jQuery(function () {
            // Создание экземпляра карты и его привязка к созданному контейнеру
            var map = new YMaps.Map(YMaps.jQuery("#YMapsID")[0]),
                
                // Центр карты
                center,
                
                // Масштаб
                zoom = 10;

            // Получение информации о местоположении пользователя
            if (YMaps.location) {
                center = new YMaps.GeoPoint(YMaps.location.longitude, YMaps.location.latitude);

                if (YMaps.location.zoom) {
                    zoom = YMaps.location.zoom;
                }

                map.openBalloon(center, "Место вашего предположительного местоположения:<br/>"
                    + (YMaps.location.country || "")
                    + (YMaps.location.region ? ", " + YMaps.location.region : "")
                    + (YMaps.location.city ? ", " + YMaps.location.city : "")
                )
            }else {
                center = new YMaps.GeoPoint(37.64, 55.76);
            }

            // Установка для карты ее центра и масштаба
            map.setCenter(center, zoom);
            
            console.log(YMaps.location.country);
            console.log(YMaps.location.region);
            console.log(YMaps.location.city);
        });
        </script>
        
        <div style="width:600px;height:400px" id="YMapsID" class="YMaps YMaps-cursor-grab"></div>
    </body>
</html>

There, after initializing YMaps, we already have the coordinates of the user, I decided to expand the spector of your question a little and made the map the same where the user's location is indicated.

If I understood correctly, you wanted to take the coordinates of the user and find out his location, after initialized map you have both the user's city and country in the YMaps properties, which you can take like this:

YMaps.location.country
YMaps.location.city

That is, if you do not need a map, then you can after initializing YMaps, you can simply take and use the data for your needs without drawing the map.

 2
Author: Raz Galstyan, 2017-05-29 12:06:43