I'm writing a mobile application in HTML5, JavaScript and CSS and converting it into an app using PhoneGap. I have a list of locations, some other data and the distance the user is from each of those locations. I have a knockout observableArray that is being populated with json information that has the following format.
[{
"Id":1,
"Name":"Field 1",
"Available":12,
"InUse":5
}]
I'm, then finding the users current location using the GeoLocation and determining how far from each of the locations he or she is. I'm using this code monitor the users position.
navigator.geolocation.watchPosition(showPosition, showError, options)
. As they walks closer to one of the fields I want to be able to display the distance they are from each field as it changes. My HTML is simple and looks like this:
<table style="width: 95%; padding: 2px; margin-left: 2px;">
<thead>
<tr style="text-align: left">
<th>Field</th>
<th>Available</th>
<th>In Use</th>
<th>Distance</th>
</tr>
</thead>
<tbody data-bind="foreach: myArray" >
<tr>
<td><span data-bind="text: Field"></span></td>
<td><span data-bind="text: Available"></span></td>
<td><span data-bind="text: InUse"></span></td>
<td><span data-bind="text: Proximity"> </span> </td>
</tr>
</tbody>
</table>
Everything is working fine and "showPosition" is being call when I move about but I don't know how to update the proximity field in the observableArray as it changes.
My showPosition method (written by a colleague) looks like this.
function showPosition(position) {
for (i = 0; i < config.geoLocs.length; i++) {
var distance =
calculateDistance(position.coords.latitude,
position.coords.longitude,
config.geoLocs[i].Lat,
config.geoLocs[i].Lng)
config.geoLocs[i].Proximity = (distance * 1000).toFixed(0) + " m";
}
}
I know that my observableArray will only be updated if it changes i.e. a new object is inserted or deleted and won't be updated if an item in it is updated. Do I have to create an observable for each object and then push that into the array or is a simple way in my "showPosition" method that I can easily iterate through the array and update each "poximity" field?
I hope all of this makes sense?
Note: The showPosition() method was not written with knockout bindings in mind. My colleague was planning on using JavaScript to find each in the table and manually update it.