【问题标题】:(Phonegap) javascript trigger event when gps location in range(Phonegap)当gps位置在范围内时javascript触发事件
【发布时间】:2015-04-27 20:32:11
【问题描述】:

我正在尝试根据特定范围内的 gps 坐标触发事件(取消隐藏警报或图像)。

我想我需要做类似的事情:如果 GPS Lat > x(根据我希望他们去的位置设置)并且 z 和

我走对了吗?我将发布我所拥有的作为让 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>

【问题讨论】:

    标签: javascript ios cordova gps location


    【解决方案1】:

    是的,看起来不错。当 GPS 轮询成功后,watcher 会调用 onSuccess,并返回数据。

    我会将经度和纬度保存在全局范围内,然后您可以对其执行任何您想要的逻辑。

    【讨论】:

    • 所以我正在重新安排一些事情以使其更符合我需要做的事情:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多