在此先感谢。我建立了一个网站,当您单击按钮“导航”时,可以选择将您引导至特定位置。一切正常工作在桌面上,并且手机的GPS启用时,一切工作正常。打开和关闭GPS时的地理位置问题
我使用谷歌地图的参考,它工作得很好。
http://maps.google.com/maps?saddr=myLat,myLng&daddr=targetLat,targetLng
我做这个$( '按钮')点击()。不在$(window).load或$(document).ready。当您点击该按钮时,它会调用地理位置功能:
function getGeolocation(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(geoSuccess, geoError);
} else {
$('.error-message').html('<p>Your browser does not support this function</p>');
}
}
function geoSuccess(position) {
myLat = position.coords.latitude;
myLng = position.coords.longitude;
window.location.href = 'http://maps.google.com/maps?saddr=' + myLat + ',' + myLng + '&daddr=targetLat,targetLng';
}
function geoError(){
switch(error.code) {
alert('example alert - enable GPS');
case error.PERMISSION_DENIED:
$('.error-message').html('<p>User denied the request for Geolocation.</p>');
break;
case error.POSITION_UNAVAILABLE:
$('.error-message').html('<p>Location information is unavailable.</p>');
break;
case error.TIMEOUT:
$('.error-message').html('<p>The request to get user location timed out.</p>');
break;
case error.UNKNOWN_ERROR:
$('.error-message').html('<p>An unknown error occurred.</p>');
break;
}
}
但是,当GPS被禁用时会出现问题。
首先,有时它不会输入geoError()。我尝试了很多次,但是我“发现”的唯一一件事是它有时会缓存。那可能吗 ?
但主要问题是,当GPS被禁用时,会弹出“示例警报”,然后打开我的GPS并再次单击该按钮,它不再工作。我必须刷新页面才能应用GPS打开。我想避免这种情况。有谁知道为什么会发生这种情况,有没有办法做到我想要的?
另一个问题是当我打开GPS打开我的页面时。然后关闭它并单击按钮。完全没有任何反应。它输入getGeolocation(),变为true,然后停止。它不输入getCurrentPosition()。有任何想法吗?