$http.get('*****').success(function(data,dealers,response)
{
 $scope.items = data;
 $scope.item ='';
 console.log(data);
var shoplatit=data.S_Location.Latitude;
 var shoplong=data.S_Location.Longitude;

});

$scope.nearme = function($scope) {
 if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function (position) {
                mysrclat = position.coords.latitude;
                mysrclong = position.coords.longitude;
                console.log(mysrclat);
                console.log(mysrclong);
        });

    }
  i tried like this but its not working getting  deg2rad is not defined erro
var lat1 =15.008809;
var lat2 =78.659096;

var lon1 =13.90457539;
var lon2 =78.5855514

  var R = 6371; // Radius of the earth in km
  var dLat = deg2rad(lat2-lat1);  // deg2rad below
  var dLon = deg2rad(lon2-lon1);
  var a =
    Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
    Math.sin(dLon/2) * Math.sin(dLon/2)
    ;
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  var d = R * c; // Distance in km
   console.log(d);

<div ng-controller="ListingCtrl" >
  <div data-ng-init="nearme()">

    <div class="list card" data-ng-repeat="item in items">
    <h2>{{item.Store_Name}} </h2>{{item.S_Location.Latitude}}<br>{{item.S_Location.Longitude}}
    <p>{{item.S_Address}}</p>
   <h1>here i want show distance in km</h1>
  </div>
    </div>
  </div>





我已经在mongodb中存储了商店的经度和纬度。在列表页面中,我使用navigator.geolocation.i获取客户的纬度和经度。我想计算两个纬度和经度之间的距离。

最佳答案

lat-long debug可以帮助您轻松调试地理距离和其他方面。

下面是计算功能的JS代码;它还具有deg2rad()函数,该函数用于将以度为单位的数字转换为等效的弧度(在您的代码中似乎不存在)。

我使用了您提供的两个坐标,返回的距离为123.04 km,也通过Google Maps进行了交叉检查以确认相同。



/*
Title: Get Distance from two Latitude / Longitude in Kilometers.

Description: This Javascript snippet calculates great-circle distances between the two points
—— that is, the shortest distance over the earth’s surface; using the ‘Haversine’ formula.
*/

function _getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) {
  var R = 6371; // Radius of the earth in kilometers
  var dLat = deg2rad(lat2 - lat1); // deg2rad below
  var dLon = deg2rad(lon2 - lon1);
  var a =
    Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
    Math.sin(dLon / 2) * Math.sin(dLon / 2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  var d = R * c; // Distance in KM
  return d;
}

function deg2rad(deg) {
  return deg * (Math.PI / 180)
}



// Point 1: 15.008809, 78.659096
// Point 2: 13.90457539, 78.5855514

var _lat1 = 15.008809;
var _lon1 = 78.659096;

var _lat2 = 13.90457539;
var _lon2 = 78.5855514;

// precise value
var _d = "Precise value: " + _getDistanceFromLatLonInKm(_lat1, _lon1, _lat2, _lon2);
console.log(_d); // alert(_d);


// round value
_d = "Round value: " +
  Math.round(_getDistanceFromLatLonInKm(_lat1, _lon1, _lat2, _lon2) * 100) / 100 +
  " km";
console.log(_d); // alert(_d);

关于javascript - 如何使用angularjs计算两个经纬度之间的距离?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31332989/

10-13 09:38