当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - 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 opts out of using the location. Is there a way to accomplish this?

网友答案:

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

网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: