【发布时间】:2016-10-02 10:42:30
【问题描述】:
我最近使用 Titanium and Alloy 开发了一个 android 应用程序。现在我正在尝试(第一次)使用比较器函数按距离对绑定的主干集合进行排序,但它不起作用。
comparator: function(game) {
var lon1, lat1, lat2, lon2;
if (Ti.Geolocation.locationServicesEnabled) {
Ti.Geolocation.getCurrentPosition(function(e) {
if (e.error) {
Ti.API.error('Error:' + e.error);
return 0;
} else {
Ti.API.info(e.coords);
lon1 = e.coords.longitude;
lat1 = e.coords.latitude;
Titanium.Geolocation.forwardGeocoder(game.get("camp"), function(e) {
if (e.success) {
lat2 = e.latitude;
lon2 = e.longitude;
var R = 6371; // km
var dLat = (lat2 - lat1) * Math.PI / 180;
var dLon = (lon2 - lon1) * Math.PI / 180;
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
console.log("KM: " + parseInt(d));
return parseInt(d);
} else {
console.log("Unable to find address");
return 0;
}
});
}
});
} else {
console.log('please enable location services')
return 0;
}
}
在我的控制器中,我使用:
var games = Alloy.Collections.allGames;
games.sort();
games.fetch();
你能告诉我有什么问题吗?
【问题讨论】:
标签: javascript backbone.js titanium titanium-alloy