Synchronize users location with google maps in java script

Is it possible to sync users' locations using geolocation? So that if a user travels, their location will update on the page, some of them, for example, navigate by their location ...

I tried this, but the whole map keeps updating and takes a while to load ... It works great, but is there a better way so that I can track users' location?

    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <script src="../dist/geolocationmarker-compiled.js"></script>
    <script>
        var map, GeoMarker;

        setInterval (function initialize() {
            var mapOptions = {
                zoom: 17,
                center: new google.maps.LatLng(-34.397, 150.644),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById('map_canvas'),
            mapOptions);

            GeoMarker = new GeolocationMarker();
            GeoMarker.setCircleOptions({fillColor: '#808080'});

            google.maps.event.addListenerOnce(GeoMarker, 'position_changed',     function() {
                map.setCenter(this.getPosition());
                map.fitBounds(this.getBounds());
            });

            google.maps.event.addListener(GeoMarker, 'geolocation_error', function(e) {
                alert('There was an error obtaining your position. Message: ' + e.message);
            });
            console.log('updating');
            GeoMarker.setMap(map);
        },10000);

    google.maps.event.addDomListener(window, 'load', initialize);

    if(!navigator.geolocation) {
        alert('Your browser does not support geolocation');
    }

</script>
<body>
    <div id="map_canvas"></div>
</body>

      

+3


source to share


1 answer


Don't re-create the map when changing position, just update the position of the marker on the map.



var map, GeoMarker;
function initialize() {
            var mapOptions = {
                zoom: 17,
                center: new google.maps.LatLng(-34.397, 150.644),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById('map_canvas'),
            mapOptions);
}
setInterval (function() {
    if (!GeoMarker) {
      GeoMarker = new GeolocationMarker();
    }
    GeoMarker.setCircleOptions({fillColor: '#808080'});

    google.maps.event.addListenerOnce(GeoMarker, 'position_changed',     function() {
            map.setCenter(this.getPosition());
            map.fitBounds(this.getBounds());
    });

    google.maps.event.addListener(GeoMarker, 'geolocation_error', function(e) {
        alert('There was an error obtaining your position. Message: ' + e.message);
    });
    console.log('updating');
    GeoMarker.setMap(map);
},10000);
google.maps.event.addDomListener(window,'load',initialize);

if(!navigator.geolocation) {
    alert('Your browser does not support geolocation');
}

      

+1


source







All Articles