0
我爲Android開發了一個帶有PhoneGap的jQuery Mobile應用程序。現在,我需要一個GPS追蹤器。我想獲取用戶的位置並將其與數據庫中的某些座標進行比較。帶jQuery手機,PhoneGap和谷歌地圖的GPS跟蹤器
我想知道,如果這種方法是好的,如果我應該改變一些東西,並且我只能一次顯示一條消息?
- 該應用程序應該檢查位置20秒鐘的間隔
- 如果用戶是目標(50米)附近,他應該得到一個消息,(但只有一次)
$(document).on("pageinit", function(event) {
function userDistance() {
// Wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);
var watchID = null;
// PhoneGap is ready
function onDeviceReady() {
watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
// Update every 10 seconds
var options = { frequency: 10000 };
watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
}
// onSuccess Geolocation
function onSuccess(position) {
// user location
var loc1 = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
// target location
$.getJSON("http://server:8080/getCoords", function(data) {
var loc2 = new google.maps.LatLng(data[0].latitude, data[0].longitude);
// distance
var distance = google.maps.geometry.spherical.computeDistanceBetween (loc1, loc2);
if (distance < 50) {
alert("target arrived");
}
});
}
// onError Callback receives a PositionError object
function onError(error) {
}
// watching position every 20s
setTimeout(userDistance, 20000);
}
userDistance();
});