Angularjs 平均堆栈:从mongodb获取数据,并在控制器文件中对此数据进行计算
我正在MEAN stack中创建一个web应用程序,在这里捐赠书籍,其他用户可以接受这些捐赠。这就是我正在关注的问题。捐赠的详细信息以以下方式存储在books表中:Angularjs 平均堆栈:从mongodb获取数据,并在控制器文件中对此数据进行计算,angularjs,node.js,mongodb,express,Angularjs,Node.js,Mongodb,Express,我正在MEAN stack中创建一个web应用程序,在这里捐赠书籍,其他用户可以接受这些捐赠。这就是我正在关注的问题。捐赠的详细信息以以下方式存储在books表中: { { "_id" : ObjectId("55d4410544c96d6f6578f893"), latitude: 15.3705074, longitude: 75.1355679, "email" : "user111@gmail.
{
{
"_id" : ObjectId("55d4410544c96d6f6578f893"),
latitude: 15.3705074,
longitude: 75.1355679,
"email" : "user111@gmail.com",
"name" : "user111",
"bookName" : "book1"
}
{
"_id" : ObjectId("58be6eb7e94fed1814f5757a"),
latitude: 15.3705074,
longitude: 75.1355679,
"email" : "user222@gmail.com",
"name" : "user222",
"bookName" : "book2"
}
{
"_id" : ObjectId("58be6a37e94fed1814f57579"),
latitude: 15.3705074,
longitude: 75.1355679,
"email" : "user333@gmail.com",
"name" : "user333",
"bookName" : "book3"
}
}
这是我的控制器文件:
angular.module('showApp', []).controller('AppCtrl',['$scope','$timeout','$http', function($scope , $timeout, $http) {
$scope.getDonations = function(curLong, curLat){
$http.get('/books/').success(function(response){
$scope.books = response;
$scope.x = "";
//how can I calculate distance here using latitude and longitude from books table and display on the webpage?
});
};
}]);
用户的当前位置将传递给GetVentials函数。我计算距离有困难。我需要计算当前用户与books表中每个条目中的位置之间的距离,并在有角度的网页上显示该距离,以便用户可以看到其移动捐赠的距离
角度代码:`
<table>
<tr>
<td>Book Name</td>
<td>Donor's name</td>
<td>Email ID</td>
<td>Distamce</td>
</tr>
<tr ng-repeat="x in books">
<td>{{x.bookName}}</td>
<td>{{x.name}}</td>
<td>{{x.email}}</td>
<td> </td> //I need to display the distance here
</tr>
</table>
书名
捐赠者姓名
电子邮件ID
距离
{{x.bookName}
{{x.name}
{{x.email}
//我需要在这里显示距离
请帮我弄清楚怎么做。谢谢:)。首先,您需要知道用户的当前位置。如果你有-- 伟大的否则你可以信赖。然后需要计算这两点之间的距离 所以让我们假设我们正在使用HTML5地理定位
function degToRad(deg) {
return deg * Math.PI / 180;
}
function distance(lon1, lat1, lon2, lat2) {
// from http://www.movable-type.co.uk/scripts/latlong.html
var R = 6371; // kilometers
var φ1 = degToRad(lat1);
var φ2 = degToRad(lat2);
var Δφ = degToRad(lat2-lat1);
var Δλ = degToRad(lon2-lon1);
var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ/2) * Math.sin(Δλ/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d;
}
function success(position) {
var userLat = position.coords.latitude;
var userLong = position.coords.longitude;
$scope.books = response.map(function (book) {
book.distanceFromUser = distance(userLong, userLat, book.longitude, book.latitude)l
return book;
});
}
navigator.geolocation.getCurrentPosition(success, function error () {
// do something....
});
你在Github或npm上搜索过计算两个长/宽点之间距离的节点模块吗?非常感谢你,这很有效!如果我想要以公里为单位的距离,R的值是多少?R是地球的半径。R=6371应以公里为单位给出答案