Cordova:HTML5地理定位 - 从JavaScript数组中找到最近的地方

时间:2014-02-27 22:03:18

标签: javascript html5 cordova geolocation gps

我在JavaScript中有一些地方。我需要从gps传感器获取gps地理位置(使用Apache Cordova在手机上)。

如果GPS精度优于例如40米,我需要做一些事情(设置css显示:阻止,改变颜色......)。

我有这段代码:

<!DOCTYPE html>
<html><head>
    <meta charset="utf-8"> 
    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script src="js/jquery-1.11.0.min.js"></script>
    <script src="js/distance.js"></script> <!-- https://github.com/janantala/GPS-distance/blob/master/javascript/distance.js -->

    <script type="text/javascript" charset="utf-8">
    var interval = 5; // [s]
    var timeout = 60; // [s]

    /* --------------------------------------------------- */
    var latitude            = new Array();
    var longtitude          = new Array();
    var nameOfLocation      = new Array();

    // address 1
    // Latitude : 10.20 | Longitude : 30.40
    latitude[0]             = 10.20;
    longtitude[0]           = 30.40;
    nameOfLocation[0]       = "address 1";

    // address 2
    // Latitude : 40.30 | Longitude : 20.10
    latitude[1]             = 40.30;
    longtitude[1]           = 20.10;
    nameOfLocation[1]       = "address 2";

    // ...
    /* --------------------------------------------------- */

    // Wait for device API libraries to load
    document.addEventListener("deviceready", onDeviceReady, false);

    // device APIs are available
    function onDeviceReady() {
        console.log('in onDeviceReady()');      
        $(document).ready(function(){
            setInterval(function(i) {
                navigator.geolocation.getCurrentPosition(onSuccess, onError, {
                    maximumAge: 0, 
                    timeout: (timeout*1000), 
                    enableHighAccuracy: true }
                );              
            }, (interval*1000))
        });        
    }

    // onSuccess Geolocation
    function onSuccess(position) {
        console.log('in onSuccess()');
        console.log(position.coords.latitude, "position.coords.latitude");
        console.log(position.coords.longitude, "position.coords.longitude");
        var element = document.getElementById('geolocation');
        element.innerHTML = 'Latitude: '           + position.coords.latitude              + '<br />' +
                            'Longitude: '          + position.coords.longitude             + '<br />' +
                            'Altitude: '           + position.coords.altitude              + '<br />' +
                            'Accuracy: '           + position.coords.accuracy              + '<br />' +
                            'Altitude Accuracy: '  + position.coords.altitudeAccuracy      + '<br />' +
                            'Heading: '            + position.coords.heading               + '<br />' +
                            'Speed: '              + position.coords.speed                 + '<br />' +
                            'Timestamp: '          + position.timestamp                    + '<br />';   

    var place;                     
    var accuracy;
    $("#accuracy").html("GPS accuracy " + position.coords.accuracy + " m.");

    if (position.coords.accuracy < 40) {
        $("#accuracy").css("background-color", "Gray");     
        for (var i=0; nameOfLocation.length; i++) {
            var distance = getDistance(latitude[0], longitude[0], position.coords.latitude, position.coords.longitude);

            if (distance <= 25) {
                place = i;
                accuracy = position.coords.accuracy;
                $("#accuracy").css("background-color", "OrangeRed");
            } else if (distance <= 20) {
                place = i;
                accuracy = position.coords.accuracy;
                $("#accuracy").css("background-color", "Yellow");
            } else if (distance <= 15) {
                place = i;
                accuracy = position.coords.accuracy;
                $("#accuracy").css("background-color", "Green");
            }               
        }

        $("#info").html("You are about <strong>" + accuracy + "</strong> meters from location <strong>" + nameOfLocation[i] + "</strong>");             
    } else {
        $("#info").html("");
    }                                
}

    // onError Callback receives a PositionError object
    function onError(error) {
        console.log('in onError()');
        console.log(error.code, "error.code");
        console.log(error.message, "error.message");
        $("#geolocation").html(
                'code: '    + error.code    + '<br />' +
                'message: ' + error.message);
        $("#accuracy").css("background-color", "");
    }    

    </script>
  </head><body>

  <p id="info"></p>  
  <hr />

  <p id="accuracy"></p>  
  <hr />  

  <p id="geolocation">GPS ...</p>

  </body></html> 

我使用这个JS lib来测量两个GPS位置https://github.com/janantala/GPS-distance/blob/master/javascript/distance.js

的距离

我不能使用google online gps distance lib。应用必须在没有互联网连接的情况下工作。

如果我运行app,则启动位置gps。在首次找到位置后,任何下一个发现只需要大约5秒钟,然后停止查找位置(这会重复到无穷大)。我需要永久搜索。

你知道哪里出错了吗?

我会做得好吗?

0 个答案:

没有答案