R
R
Roman2015-06-13 00:31:22
JavaScript
Roman, 2015-06-13 00:31:22

How to get the user's city using the Yandex.Maps API and insert it on the page?

The Yandex.Maps JavaScript API 1.x documentation provides an example of how, based on the IP address, by comparison, determine the user's estimated location and insert a map with a label on the site page.

I plan to add hidden inputs to the application form code:

<div class="response-ip-city">
<input type="hidden" name="ya-city" value=" " >
<input type="hidden" name="ya-country"   value=" " >
</div>

How to substitute the name of the city taken from the Yandex.Maps API into the value of the hidden input?
I assume API 1.1. easier and worth using it for such a task?

UPD Trying to use geolocation
Solution:
<script src="//api-maps.yandex.ru/2.0/?load=package.standard&lang=ru-RU" type="text/javascript"></script>
<script type="text/javascript">
ymaps.ready(init);
function init() {
  var geolocation = ymaps.geolocation;
  if (geolocation) {
    jQuery(function($){
      $("input[name='ya-city']").val(geolocation.city);
      $("input[name='ya-country']").val(geolocation.country);
    });
  } else {
    console.log('Не удалось установить местоположение');
  }
}
</script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Novikov, 2015-06-13
@llgruff

1.1 is easier in what sense? In such a ?
There are also simple solutions:
1) Either determine the position using HTML5 Geolocation and find out everything you need using geocoding ;
2) Either use geolocation .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question