weird geolocation error
Below is my code, and basically what I'm trying to do is calculate distance from lat/lons in the database to my current location..
i=0;
while (rows.isValidRow()) {
//var d='';
//d=distance(mylatitude,mylongitude,rows.fieldByName('lat'),rows.fieldByName('lon'));
//d=distance_plane(mylatitude,mylongitude,rows.fieldByName('lat'),rows.fieldByName('lon'),"N");
//var url='http://maps.googleapis.com/maps/api/directions/json?origin=30.5166667,-86.4822222&destination=30.7619444,-86.5705556&sensor=false';
var url='http://maps.googleapis.com/maps/api/directions/json?origin='+mylatitude+','+mylongitude+'&destination='+rows.fieldByName('lat')+','+rows.fieldByName('lon')+'&sensor=false';
req = Ti.Network.createHTTPClient();
req.open('GET', url);
req.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
req.onreadystate = function(){};
req.onload = function()
{
try {
parsedData = JSON.parse(req.responseText);
newDistance[i]=parsedData.routes[0].legs[0].distance.text;
//newDistance="distance.text " + parsedData.routes[0].legs[0].distance.text;
Ti.API.info(newDistance[i]);
//Ti.API.info("distance.value " + parsedData.routes[0].legs[0].distance.value);
//Ti.API.info(newDistance);
} catch(err) {
Ti.API.info('>>>>>>> Error In getToken ' + err );
//newDistance="Error Calculating";
}
};
req.send();
data.push({
id: rows.fieldByName('id'),
lat: rows.fieldByName('lat'),
lon: rows.fieldByName('lon'),
title: rows.fieldByName('caption') + ' - ' + newDistance[i],
info: rows.fieldByName('info'),
hasChild: true
});
//latarray[i]=rows.fieldByName('lat');
//lonarray[i]=rows.fieldByName('lon');
//titlearray[i]=rows.fieldByName('caption');
//subtitlearray[i]=rows.fieldByName('info');
i++;
// newDistance[i]='Calculating...'
rows.next();
}
rows.close();
It's the newDistance[i] thats giving me an error. I have it defined as an array elsewhere, along with the parseData, and the req variables.
With the newDistance, if I use it as a regular variable instead of an array, it works, except that all the rows get the last calculated distance. Any ideas what I could do to fix it?
1 Answer
-
Anyone? Seems like its probably something simple I'm missing, but I need another set of eyes. The output I'm getting is "undefined".