How to center google map to current location using javascript

To center google map to current user location using js api, you first have to make your website a HTTPS site

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function (position) {
        var pos = {
            lat: position.coords.latitude,
            lng: position.coords.longitude
    };
    
    map.setCenter(pos);
    }, function () {
        console.log('error gis');
    });
}

more details here

Leave a Reply

Your email address will not be published. Required fields are marked *