【发布时间】:2020-06-02 02:10:49
【问题描述】:
我有这个功能来指示是否在浏览器上启用了地理定位:
function geolocationInfo() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);
}
else {
return "Geolocation not works in this browser";
}
}
function success(position) {
var crd = position.coords;
console.log('Ваше текущее метоположение:');
console.log(`Широта: ${crd.latitude}`);
console.log(`Долгота: ${crd.longitude}`);
console.log(`Плюс-минус ${crd.accuracy} метров.`);
}
function error(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
return "Geolocation disabled";
case error.POSITION_UNAVAILABLE:
return "GeoLocation is not available";
case error.TIMEOUT:
return "Time is ends";
case error.UNKNOWN_ERROR:
return "error.";
}
}
问题是回调函数没有被触发。
知道为什么不触发回调(成功和错误)吗?
【问题讨论】:
-
通常浏览器会要求用户允许/拒绝地理定位 - 在为该站点做出或记住其中一个选择之前不会调用回调 - 那么,控制台中根本没有其他错误吗?顺便问一下,哪个浏览器?页面是 https 还是 http?
标签: javascript geolocation gps location gis