watchPosition像一個(gè)追蹤器與clearWatch成對(duì)。 watchPosition與clearWatch有點(diǎn)像setInterval和clearInterval的工作方式。 var watchPositionId = navigator.geolocation.watchPosition(success_callback, error_callback, options); navigator.geolocation.clearWatch(watchPositionId );
HTML 5提供了地理位置等一系列API可以給用戶使用,方便用戶制作LBS的地理應(yīng)用,首先在支持HTML 5的瀏覽器中,當(dāng)開啟API時(shí),會(huì)詢問是否用戶同意使用api,否則不會(huì)開啟的,保證安全。 1、開啟,判斷是否瀏覽器支持LBS api
復(fù)制代碼代碼如下: function isGeolocationAPIAvailable() { var location = "No, Geolocation is not supported by this browser."; if (window.navigator.geolocation) { location = "Yes, Geolocation is supported by this browser."; } alert(location); }
復(fù)制代碼代碼如下: function requestPosition() { if (nav == null) { nav = window.navigator; } if (nav != null) { var geoloc = nav.geolocation; if (geoloc != null) { geoloc.getCurrentPosition(successCallback); } else { alert("Geolocation API is not supported in your browser"); } } else { alert("Navigator is not found"); } }
復(fù)制代碼代碼如下: function listenForPositionUpdates() { if (nav == null) { nav = window.navigator; } if (nav != null) { var geoloc = nav.geolocation; if (geoloc != null) { watchID = geoloc.watchPosition(successCallback); } else { alert("Geolocation API is not supported in your browser"); } } else { alert("Navigator is not found"); } }
然后在successCallback中,就可以設(shè)置顯示最新的地理位置:
復(fù)制代碼代碼如下: function successCallback(position){ setText(position.coords.latitude, "latitude"); setText(position.coords.longitude, "longitude"); }
如果不希望實(shí)時(shí)跟蹤,則可以取消之: function clearWatch(watchID) { window.navigator.geolocation.clearWatch(watchID); } 4、如何處理異常 當(dāng)遇到異常時(shí),可以捕捉之:
復(fù)制代碼代碼如下: if (geoloc != null) { geoloc.getCurrentPosition(successCallback, errorCallback); } function errorCallback(error) { var message = ""; switch (error.code) { case error.PERMISSION_DENIED: message = "This website does not have permission to use " + "the Geolocation API"; break; case error.POSITION_UNAVAILABLE: message = "The current position could not be determined."; break; case error.PERMISSION_DENIED_TIMEOUT: message = "The current position could not be determined " + "within the specified timeout period."; break; } if (message == "") { var strErrorCode = error.code.toString(); message = "The position could not be determined due to " + "an unknown error (Code: " + strErrorCode + ")."; } alert(message); }
復(fù)制代碼代碼如下: function getCurrentLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showMyPosition,showError); } else { alert("No, Geolocation API is not supported by this browser."); } } function showMyPosition(position) { var coordinates=position.coords.latitude+","+position.coords.longitude; var map_url="http://maps.googleapis.com/maps/api/staticmap?center=" +coordinates+" zoom=14 size=300x300 sensor=false"; document.getElementById("googlemap").innerHTML=" img src='"+map_url+"' / } function showError(error) { switch(error.code) { case error.PERMISSION_DENIED: alert("This website does not have permission to use the Geolocation API") break; case error.POSITION_UNAVAILABLE: alert("The current position could not be determined.") break; case error.TIMEOUT: alert("The current position could not be determined within the specified time out period.") break; case error.UNKNOWN_ERROR: alert("The position could not be determined due to an unknown error.") break; } } html教程