Skip to content Skip to sidebar Skip to footer

Google Map - User Choses Not To Allow Location

I am faced with a situation where the user does not want to allow the system to use the google location api to center the map on them. I need to set a default value if the user op

Solution 1:

Yes there is! Google maps uses navigator.geolocation.getCurrentPosition() method to get your current location. This method allows a success callback (when a user says yes) and error callback (when user says no). It is also good to check if the browser supports geolocation. The following shows how to use it

if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(hasGeolocation, noGeolocation)
        } else {
            geolocationNotSupported();
        }

        functionhasGeolocation() {
            console.log("yeah!")
        }

        functionnoGeolocation() {
            console.log("user said no!")
        }

        functiongeolocationNotSupported() {
            console.log("this browser does not support geolocation")
        }

There is also an example on google maps api docs to show this in context of google maps usage https://developers.google.com/maps/articles/geolocation

Solution 2:

when you are initializing your map you are defining map options. Just define the 'center' option in this moment.

Post a Comment for "Google Map - User Choses Not To Allow Location"