尝试根据设备的地理位置对JSON文件进行排序。
JSON文件:
jsonCallback(
[
{
"codelocatie":"AL0008",
"codecontainer":"GAL0008444",
"gemeente":"Almelo",
"straatnaam":"Bornerbroeksestraat",
"omschrijving":"bij Aldi",
"model":"BG 3000",
"soort":"GLAS",
"gps-x":"52.3497120",
"gps-y":"6.66140500"
},
{
"codelocatie":"AL0010",
"codecontainer":"GAL0010005",
"gemeente":"Almelo",
"straatnaam":"Bornsestraat 48",
"omschrijving":"park.pl. Plusmarkt",
"model":"OG 4000",
"soort":"GLAS",
"gps-x":"52.3512100",
"gps-y":"6.66672100"
},
{
"codelocatie":"AL0010",
"codecontainer":"KAL0010003",
"gemeente":"Almelo",
"straatnaam":"Bornsestraat 48",
"omschrijving":"park.pl. Plusmarkt",
"model":"OG 5000",
"soort":"KVM",
"gps-x":"52.3512200",
"gps-y":"6.66672900"
},
{
"codelocatie":"AL0010",
"codecontainer":"KAL0010004",
"gemeente":"Almelo",
"straatnaam":"Bornsestraat 48",
"omschrijving":"park.pl. Plusmarkt",
"model":"OG 5000",
"soort":"KVM",
"gps-x":"52.3512200",
"gps-y":"6.66672900"
}
]
);
设备的地理位置我存储在div中。
这就是我目前获取JSON数据的方式
.controller('afvalbakController' ,['$scope', '$http',
function afvalbakController($scope, $http) {
$.ajax({
type : "GET",
contentType: "application/json; charset=utf-8",
async: false,
url : 'http://www.lans.ink/TABLE_3.json?callback=?',
dataType: "jsonp",
jsonpCallback: 'jsonCallback',
success: function(data) {
$scope.contents = data;
},
error: function(e) {
console.log(e.message);
}
})
}])
它目前只抓取第一个,然后是第二个等。
我希望它按地理位置排序,所以最接近,但到目前为止没有运气。 这可以实现吗?如果是这样,怎么样?
答案 0 :(得分:1)
在您的位置数组上循环,并检查每个点与地理位置点之间的距离并对其进行排序。这是一个检查2点之间距离的函数。
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; }
return dist;
}