javascript - (Phonegap) 当 GPS 位置在范围内时触发 JavaScript 事件

标签 javascript ios cordova gps location

我正在尝试根据落在特定范围内的 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/

相关文章:

javascript - Angular 2-再次单击routerLink时重新加载组件

javascript - 忽略空白值对 json 进行排序

javascript - HTML5 canvas 绘制一个带有明胶效果的矩形

iOS 国际化与下载的图像

android - Email Composer 插件不适用于带有 cordova 的 Android 6 (Marsh Mallow)

javascript - 如何使用 Javascript 解释器切换到 Jmeter Selenium 中的新选项卡?

ios - 我怎样才能让我的segues更顺利地过渡?

ios - 振动和警报在 iOS 中不起作用

node.js - 安装 Ionic - 无法识别术语 'ionic'

css - 某种不需要的 div 突出显示 datepicker materialize-css