【问题标题】:GPS tracker with jQuery mobile, PhoneGap and Google MapsGPS 追踪器与 jQuery 移动、PhoneGap 和谷歌地图
【发布时间】: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


    【解决方案1】:

    不用勺子喂代码,逻辑很简单。

    1) 在 global/root notify=0 中创建一个变量。该变量将告诉您的函数是否已通知用户 2)在您的警报例程中,添加以下代码

    if (distance < 50 and **notified==0**) {    
                **notified=1;** // tells your app that the user has been notified already, and wont alert again
                alert("target arrived");
    
            }
    

    3) 在您的 CHECK distance 例程中,添加一个评估 when distance > 50, reset the variable , notify = 0

    这个逻辑解决了问题: 1) 当用户在地图的目标距离/半径(50m)内时,仅通知用户一次 2) 如果用户离开了目标半径,会在进入该半径时再次通知一次。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2013-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      • 1970-01-01
      相关资源
      最近更新 更多