我有一个 couchdb,其中包含事件的开始时间和坐标。我写了一个列表视图,计算从您当前位置到这些事件的距离,如下所示:
locateEvents: function(head, req){
var row, comma = '';
start({
"headers": {
"Content-Type": "application/json"
}
});
if(req.query.latitude&&req.query.longitude&&req.query.radius&&req.query.now){
var R = 6371; // km
var dLon, dLat, lat1, lat2;
var results = [];
while(row = getRow()) {
dLon = Math.abs(row.value.venue.longitude-req.query.longitude);
dLat = Math.abs(row.value.venue.latitude-req.query.latitude);
dLon = (dLon*3.14159)/180;
dLat = (dLat*3.14159)/180;
lat1 = (Math.abs(req.query.longitude)*3.14159)/180;
lat2 = (Math.abs(req.query.latitude)*3.14159)/180;
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
if((d < req.query.radius)&&(row.value.start_time > req.query.now)){
results.push(row.value);
}
}
send(JSON.stringify(results));
}else{
start({"code": 500});
send("Latitude, longitude, page and radius parameters should be provided. i.e: latitude=value&longitude=value&radius=value&now=value");
}
我有一个简单的 byDate 事件视图,如下所示:
byDate: {
map: function(doc){ if (doc.resource === 'Event') {emit(doc.venue.start_time, doc);}}
}
我的担忧:有没有办法先按列表中的距离对事件进行排序,然后按开始时间对排序列表进行排序?