【问题标题】:improve geo location with jquery in html5?在 html5 中使用 jquery 改善地理位置?
【发布时间】:2019-11-11 02:40:28
【问题描述】:

我有以下完整代码正在运行.. 该脚本每 3 秒检索一次行驶中的汽车的纬度和经度,并将数据保存到 mysql 表中。 即使在手机上运行 GPS,精度也不是很好。 我添加了 Options Var,以便我可以 enableHighAccuracy: true

但是我画地图的时候准确率还是很差:

有什么办法可以改善吗?

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <title></title>
    <script type="text/javascript" src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js?ver=3.3.1'></script>
</head>
<body style="text-align: center">
    <button style="font-size:30px" id="find_btn">Find Me</button>
    <div id="result"></div>
    <br>
    <div id="resp"></div>
    <br>

    <button style="font-size:30px" id="stop_btn">STOP</button>

    <script type="text/javascript">
    function SaveLatLng(lat,lng) {
        var url = "saveLatLng.php";
        $.ajax({                        
           type: "POST",                 
           url: url,                     
           data: {latitude:lat,longitude:lng}, 
           success: function(data) {
               $('#resp').html(data);               
           }
       });
    };

    var options = {
        enableHighAccuracy: true,
        timeout: 5000,
        maximumAge: 0
    };

    function error(err) {
        console.warn(`ERROR(${err.code}): ${err.message}`);
    }

    function randomQuote() { //user clicks button
        if ("geolocation" in navigator) { //check geolocation available 
            //try to get user current location using getCurrentPosition() method
            navigator.geolocation.getCurrentPosition(function(position){ 
                $("#result").html("Found your location <br />Lat : "+position.coords.latitude+" </br>Lang :"+ position.coords.longitude);
                SaveLatLng(position.coords.latitude,position.coords.longitude);
            }, error, options);
        } else {
            console.log("Browser doesn't support geolocation!");
        }
    };

    var interval = null;
    $("#find_btn").click(function () { 
        interval = setInterval(randomQuote, 3000);
    });    

    $("#stop_btn").click(function () { 
        clearInterval(interval);
    });
</script>
</body>
</html>

【问题讨论】:

    标签: javascript jquery geolocation gps coordinates


    【解决方案1】:

    每 3 秒使用一次 watchPosition 而不是 getCurrentPosition。后者经过优化,以可能牺牲准确性为代价快速返回。您也可以检查返回值的准确性(以米为单位)并确定它太不准确并忽略它。

    您可能还需要考虑其他详细的位置过滤here 最小距离、最小时间等

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 2012-08-16
      • 2014-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多