2015-07-10 124 views
1

如何使用angulajs計算兩個緯度和經度之間的距離?

$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; 
 
\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t 
 
}); 
 

 
$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獲取客戶緯度和語言。我想計算兩個經度和緯度之間的距離。我已附上我的代碼

回答

2

lat-long debug可以幫助您輕鬆調試地理距離和其他方面。

下面是計算它的功能性JS代碼;它也有deg2rad()函數,用於將度數轉換爲弧度等值(在您的代碼中似乎丟失)。

我已經使用了您提供的兩個座標,返回的距離爲123.04 km,也通過谷歌地圖進行交叉檢查以驗證相同。

/* 
 
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);

+0

感謝它的工作,但我想在該項目中顯示距離我已經在html中提到如何做到這一點,請幫助我 –

0

考慮到你提到你的位置的數據,你似乎試圖列出「近」的裝置來進行查詢當前位置,那麼你要對這個都是錯的。

MongoDB支持地理空間查詢,當然你的數據格式正確。從這裏您可以讓「服務器」爲您進行距離計算,而不是試圖計算客戶端代碼中兩點之間的距離。它只需要一點點設置。

假設你遵循了「中庸」堆東西,因此使用服務器的NodeJS上貓鼬(當然你不好嗎?),那麼你可能會想建立一個架構是這樣的:

var placeSchema = new Schema({ 
    "name": String, 
    "location": { 
     "type": String, 
     "coordinates": [] 
    } 
}); 

placeSchema.index({ "location": "2dsphere" }) 
module.exports = mongoose.model("Place",placeSchema); 

設置了您的收藏「地點」與一些數據,最重要的是「位置」字段存儲在GeoJSON格式。遵循該標準是一個好主意,因爲它在許多API中得到了廣泛的支持,並且很適合在全球範圍內獲得「正確」距離。

你會看到的另一件事是"2dsphere"索引。這將用於您將使用的地理空間查詢類型,以便查找「靠近」您正在查詢的位置的內容。

集合中的數據,那麼應該是這個樣子:

{ 
    "name": "Sydney", 
    "location": { 
     "type": "Point", 
     "coordinates": [150.9224326,-33.7969235] 
    } 
} 

注意座標的順序這裏是「經度」,那麼「緯度」,以有效。

現在您只需要在控制器代碼(服務器)中執行MongoDB方法來查詢數據。爲了返回「距離」,使用的操作必須返回「距離」字段。有兩種選擇:geoNear數據庫命令,其中mongoose提供了一個包裝器,或者$geoNear彙總框架運算符。我們將堅持聚合運營商,因爲這裏有更多的靈活性。

var coordinates = [150.9418357,-33.7775636] 

Place.aggregate(
    [ 
     { "$geoNear": { 
      "near": { 
       "type": "Point", 
       "coordinates": coordinates 
      }, 
      "distanceField": "distance", 
      "distanceMultiplier": 0.001, 
      "spherical": true 
     }} 
    ], 
    function(err,results) { 
     // send results in the response 
    } 
); 

將從查詢點,並返回結果都是「有序」通過最近距離的「距離」計算公里:

{ 
    "_id" : ObjectId("559f68366274d659c90d02b8"), 
    "name" : "Parramatta", 
    "location" : { 
     "type" : "Point", 
     "coordinates" : [ 151.0082989, -33.8151255 ] 
    }, 
    "distance" : 8.255873925464623 
}, 
{ 
    "_id" : ObjectId("559f68366274d659c90d02b7"), 
    "name" : "Sydney", 
    "location" : { 
     "type" : "Point", 
     "coordinates" : [ 151.2094, -33.865 ] 
    }, 
    "distance" : 27.46479097844148 
} 

因此,有你的服務問題您導航座標到您的服務器API(按正確順序),然​​後發出查詢以返回結果。然後可以將響應數據輸出到您的模板,而無需計算客戶端代碼中的距離。

不可能比這更簡單。

相關問題