How to bind a location button to your html element (yandex map api)?
-
Good day!
Actually, there is a location button on the Yandex map. I want to bind a button to my html element and locate it on click.
Tell me how to do it? There is a method of automatic detection when loading a map, but it is not very suitable. Need to click.
ymaps.ready(init); function init() { var geolocation = ymaps.geolocation, myMap = new ymaps.Map('map', { center: [55.77036255174332, 38.20846108398434], zoom: 9, controls: ['default'] }, { searchControlProvider: 'yandex#search' }); geolocation.get({ provider: 'browser', mapStateAutoApply: true }).then(function (result) { // Синим цветом пометим положение, полученное через браузер. // Если браузер не поддерживает эту функциональность, метка не будет добавлена на карту. result.geoObjects.options.set('preset', 'islands#blueCircleIcon'); myMap.geoObjects.add(result.geoObjects); }); }
JavaScript Anonymous, May 22, 2019 -
<div class="map_btn" id="geo">Определить местоположение</div>
var a = document.getElementById('geo');
a.onclick = function() {
geolocation.get({
mapStateAutoApply: true
})
.then(
function(result) {
// Получение местоположения пользователя.
var userAddress = result.geoObjects.get(0).properties.get('text');
var userCoodinates = result.geoObjects.get(0).geometry.getCoordinates();
result.geoObjects.get(0).properties.set({
balloonContentBody: 'Адрес: ' + userAddress +
'<br/>Координаты:' + userCoodinates
});
myMap.geoObjects.add(result.geoObjects)
},
function(err) {
console.log('Ошибка: ' + err)
}
);
}Wyatt Norman
1 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!