我正在尝试使用HTML5的明胶定位,以获得当前游客的位置。由于隐私,浏览器将提示用户允许/不允许与站点共享他们的位置。
如果用户不允许共享他们的位置,我希望向控制台显示一条消息。
以下是我所做的
$(function() {
// Try HTML5 geolocation.
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(function(position)
{
findClosestStore(position.coords.latitude, position.coords.longitude);
}, displayMapWithAddressBar);
}
else
{
// Browser doesn't support Geolocation
console.log('browser does not support geolocation!')
displayMapWithAddressBar();
}
});
function displayMapWithAddressBar()
{
console.log('Show the map since the location was not detected!');
}但是当用户拒绝分享他们的位置时,我在控制台上看不到任何东西。我希望看到一条消息说是Show the map since the location was not detected!
我怎么知道用户拒绝分享他们的位置?
更新
我的代码在google中的工作就像预期的那样,但在Firefox中却没有。我怎样才能使它在所有浏览器中工作?
发布于 2016-10-25 17:24:17
没有办法区分他们是否拒绝了许可,或者他们的浏览器是否完全不兼容(至少在Firefox中是这样)。这样做的方法是用地址栏显示地图,或者任何默认行为,然后在他们接受的时候更新UI。这个代码更简单..。
displayMapWithAddressBar();
navigator.geolocation.getCurrentPosition(function(position){
findClosestStore(position.coords.latitude, position.coords.longitude);
});https://stackoverflow.com/questions/40245696
复制相似问题