谷歌地图 - 用户选择不允许位置 [英] google map - user choses not to allow location

查看:112
本文介绍了谷歌地图 - 用户选择不允许位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临的情况是,用户不想让系统使用Google地理位置api将地图居中。



如果用户选择退出使用位置,我需要设置默认值。有没有办法做到这一点?

解决方案

是的! Google地图使用navigator.geolocation.getCurrentPosition()方法来获取当前位置。此方法允许成功回调(当用户说是)和错误回调(当用户说不)时。检查浏览器是否支持地理位置也很好。下面显示了如何使用它

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


函数hasGeolocation(){
console.log(yeah!)
}

函数noGeolocation(){
console.log(user said no!)
}

函数geolocationNotSupported(){
console.log(此浏览器不支持地理定位)
}

谷歌地图上还有一个例子api docs在google上下文中显示映射使用情况
https://developers.google.com/maps/articles/geolocation


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

这篇关于谷歌地图 - 用户选择不允许位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆