How to determine the geolocation of the user? As I understand it is done through

window.navigator.geolocation 

But so far nothing intelligible I could not extract

    1 answer 1

    HTML

     <div id="map"></div> <script async defer src="https://maps.googleapis.com/maps/api/js? key=ТВОЙ_АПИ_КЛЮЧ&callback=initMap"> </script> 

    CSS

     #map { height: 100%; } html, body { height: 100%; margin: 0; padding: 0; } 

    Js

     <script> function initMap() { var map = new google.maps.Map(document.getElementById('map'), { center: {lat: -34.397, lng: 150.644}, zoom: 6 }); var infoWindow = new google.maps.InfoWindow({map: map}); // Пробуем с помощью HTML5 if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { var pos = { lat: position.coords.latitude, lng: position.coords.longitude }; infoWindow.setPosition(pos); infoWindow.setContent('Location found.'); map.setCenter(pos); }, function() { handleLocationError(true, infoWindow, map.getCenter()); }); } else { // Браузер не поддерживает геолокацию handleLocationError(false, infoWindow, map.getCenter()); } } function handleLocationError(browserHasGeolocation, infoWindow, pos) { infoWindow.setPosition(pos); infoWindow.setContent(browserHasGeolocation ? 'Error: The Geolocation service failed.' : 'Error: Your browser doesn\'t support geolocation.'); } </script> <script async defer src="https://maps.googleapis.com/maps/api/js?key=СЮДА_СВОЙ_АПИ_КЛЮЧ&callback=initMap"> </script> 

    But we need api from google (get quite simple)