我正在使用php创建移动webapp。我需要获取当前用户位置,即用户访问webapp的位置。我使用了HTML5 Geolocation,如下所示: -
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function(position) {
alert("Lat: " + position.coords.latitude + "\nLon: " + position.coords.longitude);
},
function(error){
alert(error.message);
switch(error.code) {
case error.PERMISSION_DENIED:
alert("User denied the request for Geolocation.");
break;
case error.POSITION_UNAVAILABLE:
alert("Location information is unavailable.");
break;
case error.TIMEOUT:
alert("The request to get user location timed out.");
break;
case error.UNKNOWN_ERROR:
alert("An unknown error occurred.");
break;
}
}, {
enableHighAccuracy: true
,timeout : 5000
} );
} else {
$('#demo').html('Geolocation is not supported by this browser.');
}
但上面的代码不起作用。在Firefox中它会抛出
未知错误获取位置
并在chrome中我收到错误消息
用户拒绝了地理位置请求
我已检查并且已在浏览器设置中启用了位置。我需要在移动设备中使用应用程序,所以是否有任何类型的检查可以添加到javascript或PHP中,要求用户启用位置。