Order tableviewrow by calculated distance

You must Login before you can answer or comment on any questions.

Hi guys,

can anyone help me out with ordering tableviewrows by calculated distance.

I've got a while loop where I fetch all the data from my database. Inside my while loop I calculate the distance for every place inside my database based on their fixed coordinates and the user's location.

var rows = db.execute('SELECT DISTINCT p.placesID, p.name, p.longitude, p.latitude FROM places p, categories c, places_categories pc WHERE pc.categoryID="' + hasCategoryID + '"AND pc.placesID = p.placesID ORDER BY name');
var dataArray = [];
while (rows.isValidRow()) {
    var row = Titanium.UI.createTableViewRow({
        className:'placesRow',
        placesID:rows.fieldByName('placesID'),
        title:rows.fieldByName('name'),
        rowDistance:' ',
    });
    var fixLat = rows.fieldByName('latitude');
    var fixLon = rows.fieldByName('longitude');
    var longitude = e.coords.longitude;
    var latitude = e.coords.latitude;
    if (fixLon==='' || fixLat===''){
        // If coordinates are not inside the database don't calculate distance
     } else {
        // Here I calculate the distance inside a label and add that label to the tableviewrow
        var distance =  Titanium.UI.createLabel({
            text:getDistance(fixLat,fixLon,latitude,longitude).toFixed(0) + 'm',
            distance:getDistance(fixLat,fixLon,latitude,longitude),
        });
        // Here I pass the value of the distance to rowDistance
        row.rowDistance = distance.distance;
        row.add(distance);
    }
    dataArray.push(row);
    rows.next();
    Ti.API.info(points);
};
tableview.setData(dataArray);
rows.close();
db.close();
I would like to be able to re-order my table based on rowDistance inside my row variable so the user can see what the nearest location is. Anyone have any suggestions?

Thanks!

— asked 2 years ago by Elon Mulder
0 Comments

1 Answer

I think there are two options. You can make the calculation in SQLite.

var rows = db.execute('SELECT DISTINCT p.placesID, p.name, p.longitude, p.latitude FROM places p, categories c, places_categories pc WHERE pc.categoryID="' + hasCategoryID + '"AND pc.placesID = p.placesID ORDER BY SQRT((p.latitude - ' + latitude + ')^2 - (p.longitude - ' + longitude +  ')^2) ');
or you can make the calculation in javascript after retrive rows, It's easy using functional programming (In this example I'm using underscore):
var rows = db.execute('<your query>')
 
rows = _.sortBy(rows, function(row) { 
    return getDistance(rows.fieldByName('latitude'),
            rows.fieldByName('longitude'),latitude,longitude) 
});
 
tableview.setData(rows);

— answered 2 years ago by Luis Cruz
answer permalink
0 Comments

Your Answer

Think you can help? Login to answer this question!

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.