【发布时间】:2014-02-17 04:30:34
【问题描述】:
我已经使用 Android 的 PhoneGap 开发了一个 jQuery Mobile 应用程序。现在,我需要一个 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();
});
【问题讨论】:
标签: android jquery google-maps jquery-mobile cordova