簡體   English   中英

如何獲取經度和緯度以進行JavaScript計算?

[英]How to get latitude and longitude to do calculations in javascript?

我需要在網絡瀏覽器中獲取用戶的緯度和經度坐標,以計算用戶與各種餐廳之間的距離。 為此,首先我需要緯度和經度,

我一直在使用此代碼來獲取經緯度,但是我似乎無法保存actaul值來進行計算,而且似乎也有很多人也遇到了這個問題,在進行了大量搜索之后,我仍然尚未找到解決方案。

  <script type="text/javascript"> var jArray= <?php echo json_encode($restaurant); ?>; //global variable var userLat; var userLng; var acc; if(!navigator.geolocation, jArray){ alert('Your Browser does not support HTML5 Geo Location. Please Use Newer Version Browsers'); } navigator.geolocation.getCurrentPosition(success, error, jArray); console.log(jArray); function success(position, jArray) { var latitude = position.coords.latitude; var longitude = position.coords.longitude; var accuracy = position.coords.accuracy; document.getElementById("lat").value = latitude; document.getElementById("lng").value = longitude; document.getElementById("acc").value = accuracy; //updating global variable with user's latitude longitude userLat = latitude; userLng = longitude; acc = accuracy; //console.log(userLat) console.log(jArray); //var ulat = $('#lat').val(); //var ulongi = $('#lng').val(); //call your distance function here ie after success, then only there will be values. //distance(ulat, userLng ,28.459768, 77.05169939999999,"K"); } function error(err){ alert('ERROR(' + err.code + '): ' + err.message); } function distance(lat1, lon1, lat2, lon2, unit) { var radlat1 = Math.PI * lat1/180 var radlat2 = Math.PI * lat2/180 var radlon1 = Math.PI * lon1/180 var radlon2 = Math.PI * lon2/180 var theta = lon1-lon2 var radtheta = Math.PI * theta/180 var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta); dist = Math.acos(dist) dist = dist * 180/Math.PI dist = dist * 60 * 1.1515 if (unit=="K") { dist = dist * 1.609344 } if (unit=="N") { dist = dist * 0.8684 } console.log(dist) return dist } </script> 


讓我知道如何從瀏覽器全局存儲緯度和經度,謝謝

好的,所以我假設在您的函數中,距離lat1是用戶的緯度,而lon1是用戶的經度。

我們可以用您的代碼執行的最簡單的操作是將lat long存儲在全局變量中。 這是通過使用腳本標簽在所有函數外部聲明變量來完成的。

下面是代碼示例。 嘗試弄清楚事情是如何執行的。

//global variable
    var userLat;
    var userLng;
    var acc;

    if(!navigator.geolocation){
        alert('Your Browser does not support HTML5 Geo Location. Please Use Newer Version Browsers');
    }
    navigator.geolocation.getCurrentPosition(success, error);
    function success(position){
        var latitude  = position.coords.latitude;   
        var longitude = position.coords.longitude;  
        var accuracy  = position.coords.accuracy;
        document.getElementById("lat").value  = latitude;
        document.getElementById("lng").value  = longitude;
        document.getElementById("acc").value  = accuracy;

//updating global variable with user's latitude longitude
        userLat = latitude;
        userLng  = longitude;
        acc = accuracy;
    //call your distance function here i.e after success, then only there will be values.
     distance(28.459768, 77.05169939999999);
    }
    function error(err){
        alert('ERROR(' + err.code + '): ' + err.message);
    }


    //this code will be used to calculate distance between user and restaurants. I have restaurant latitude and longitude in a javascript array and have 227 instances
        function distance(lat2, lon2, unit) {
//making use of global variable
        var radlat1 = Math.PI * userLat/180
        var radlat2 = Math.PI * lat2/180
        var radlon1 = Math.PI * userLng/180
        var radlon2 = Math.PI * lon2/180
        var theta = userLng-lon2
        var radtheta = Math.PI * theta/180
        var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
        dist = Math.acos(dist)
        dist = dist * 180/Math.PI
        dist = dist * 60 * 1.1515
        if (unit=="K") { dist = dist * 1.609344 }
        if (unit=="N") { dist = dist * 0.8684 }
        return dist
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM