在此先感謝。我建立了一個網站,當您單擊按鈕「導航」時,可以選擇將您引導至特定位置。一切正常工作在桌面上,並且手機的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()。有任何想法嗎?