google map - user choses not to allow location

前端 未结 2 1184
一个人的身影
一个人的身影 2021-01-29 06:20

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 v

2条回答
  •  抹茶落季
    2021-01-29 06:26

    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();
            }
    
            function hasGeolocation() {
                console.log("yeah!")
            }
    
            function noGeolocation() {
                console.log("user said no!")
            }
    
            function geolocationNotSupported() {
                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

提交回复
热议问题