JSFiddle - React, Tailwind, and code Playground
by Andrey
HTML
<script src="https://api-maps.yandex.ru/2.1/?lang=ru_RU"></script>
<!DOCTYPE html>
<html>
<head>
<title>Определение местоположения пользователя</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--
Укажите свой API-ключ. Тестовый ключ НЕ БУДЕТ работать на других сайтах.
Получить ключ можно в Кабинете разработчика: https://developer.tech.yandex.ru/keys/
-->
<script src="https://api-maps.yandex.ru/2.1/?lang=ru_RU&apikey=<ваш API-ключ>" type="text/javascript"></script>
<script src="geolocation.js" type="text/javascript"></script>
<style>
html, body, #map {
width: 100%; height: 100%; padding: 0; margin: 0;
}
</style>
</head>
<body>
<div id="map"></div>
</body>
</html>
JavaScript
ymaps.ready(init);
function init() {
var geolocation = ymaps.geolocation,
myMap = new ymaps.Map('map', {
center: [55, 34],
zoom: 10
}, {
searchControlProvider: 'yandex#search'
});
// Сравним положение, вычисленное по ip пользователя и
// положение, вычисленное средствами браузера.
geolocation.get({
provider: 'yandex',
mapStateAutoApply: true
}).then(function (result) {
// Красным цветом пометим положение, вычисленное через ip.
result.geoObjects.options.set('preset', 'islands#redCircleIcon');
result.geoObjects.get(0).properties.set({
balloonContentBody: 'Мое местоположение'
});
myMap.geoObjects.add(result.geoObjects);
console.log(result.geoObjects)
});
}