如何获得经度和经度在javascript中进行计算?

时间:2014-12-01 07:01:19

标签: javascript google-maps

我需要在webrowser中获取用户的纬度和经度坐标,以计算用户与各餐馆之间的距离。 要做到这一点,首先我需要纬度和经度,

我一直在使用这段代码来获取lat和long,但我似乎无法保存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 i.e 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>
&#13;
&#13;
&#13;


让我知道如何在全球范围内存储浏览器的纬度和经度, 感谢

1 个答案:

答案 0 :(得分:0)

好的,所以我假设在你的函数中,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
    }