我正在尝试根据落在特定范围内的 GPS 坐标来触发事件(以取消隐藏警报或图像)。
我想我需要做类似的事情:如果 GPS Lat 是 > x (根据我希望他们去的位置设置)和 < y & GPS Long 是 > z 和 < a,然后显示(使用 js 更改 css显示: block )。
我走的路正确吗?我将发布我所拥有的作为让 GPS 坐标出现的基础知识。我一边走一边学习,因此对于正确的结构,我们将不胜感激。谢谢。
document.addEventListener("deviceready", onDeviceReady, false);
var watchID = null;
// device APIs are available
//
function onDeviceReady() {
var options = { timeout: 100000 };
watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
}
// onSuccess Geolocation
//
function onSuccess(position) {
var element = document.getElementById('geolocation');
element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
'Longitude: ' + position.coords.longitude + '<br />' +
'<hr />' + element.innerHTML;
}
// onError Callback receives a PositionError object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
更新:
我已经达到了没有收到错误的地步,现在我希望得到一些帮助来根据我当前的位置和目的地获得标题。任何帮助是极大的赞赏。我在下面发布我当前的代码:
document.addEventListener("deviceready", onDeviceReady, false);
var watchID = null;
var gpscoord = null;
var destLat = 37.200401
var destLon = 93.278610
function onDeviceReady() {
var gpsOptions = { timeout: 5000 };
gpscoord = navigator.geolocation.getCurrentPosition(gpsSuccess, gpsError, gpsOptions);
}
//gpsSuccess
//
function gpsSuccess(position) {
var lat = document.getElementById('latitude');
var lon = document.getElementById('longitude');
lat.innerHTML = 'Latitude:' + position.coords.latitude;
lon.innerHTML = 'Longitude:' + position.coords.longitude;
document.getElementById('gotoLat').innerHTML = 'Destination Latitude: ' + destLat;
document.getElementById('gotoLon').innerHTML = 'Destination Longitude: ' + destLon;
}
function gpsError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
<div class="container">
<br/>
<br/>
<p id="latitude">Watching latitude...</p>
<p id="longitude">Watching longitude...</p>
<br/>
<p id="gotoLat">Destination latitude...</p>
<p id="gotoLon">Destination longitude...</p>
</div>
最佳答案
是的,看起来不错。当 GPS 成功轮询后,watcher 将调用 onSuccess,并返回数据。
我会将经度和纬度保存在全局范围内,然后您可以对其执行任何类型的逻辑。
关于javascript - (Phonegap) 当 GPS 位置在范围内时触发 JavaScript 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29905394/