I am attempting to sort a knockout observable array using geo-points according to proximity to the users location. I have a function that loops through all the stores in my array and finds the closest marker to the users current location. Then im nesting that inside another loop, using insertion sort to sort all the elements.
I have two issues. First. My swap method is a bit funky.I think its breaking the dom. Dont think i understand how to swap elemts correctly in a knockout observable.
Second. Is this even the right approach? Ko observable array has a built in sort method but im not sure how to implement it using the closest point to the user function. Ive attached the code below. Any help or insight would be appreciated.
var stores = ko.observableArray();
storesRepository.getFeed(stores);
function closestMarker(lat, lng)
{
var pi = Math.PI;
var R = 6371; //equatorial radius
var lat1 = lat;
var lon1 = lng;
var distances, closest, min, chLat, chLon, dLat, dLon, rLat1, rLat2, a, c, d;
for (j = 0; j < stores().length; j++) { // outer loop uses insertion sort to "sort" elements.
distances = [];
closest = -1;
min = 0;
for (i = j+1; i < stores().length; i++) { // inner loop finds closest marker to user
var lat2 = stores()[i].latitude();
var lon2 = stores()[i].longitude();
chLat = lat2 - lat1;
chLon = lon2 - lon1;
dLat = chLat * (pi / 180);
dLon = chLon * (pi / 180);
rLat1 = lat1 * (pi / 180);
rLat2 = lat2 * (pi / 180);
a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(rLat1) * Math.cos(rLat2);
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
d = R * c;
distances[i] = d;
if (closest == -1 || d < distances[closest]) {
closest = i;
}
}
swap(j, closest);
}
function swap(a, b) { // i dont think this is the right approach
// alert("working");
var temp = stores()[b];
stores.replace(stores()[b],stores()[a]);
stores.replace(stores()[a], temp);
}
}
return stores;