Titanium.Geolocation.getCurrentPosition interacting with SQL database
I am trying my best to figure out how to do this, I want to show a list from a sqlite database based on the users location. I have the code to do this, and it works fine with a javascript array, but as soon as I add in the database, my app crashes.
I need to be able to get the longitude/latitude variables from the Titanium.Geolocation.getCurrentPosition function outside to other functions, is this possible?
Here is what I have so far:
Ti.Geolocation.purpose = "Recieve User Location";
Titanium.Geolocation.getCurrentPosition(function(e) {
if (e.error) {
Ti.API.log('error: ' + JSON.stringify(e.error) );
return;
}
var db = Titanium.Database.install('places.db', 'places');
db = Titanium.Database.open ('places');
var rows = db.execute('SELECT * FROM places order by title');
var earthRadius = 6370.998685023; // Radius of earth in km
var currentLatitude = e.coords.latitude;
var currentLongitude = e.coords.longitude;
var distanceKM = 30 / 0.621;
var dataDistance = [];
while(rows.isValidRow())
{
var latitudeFrom = currentLatitude * (Math.PI / 180);
var longitudeFrom = currentLongitude * (Math.PI / 180);
var latitudeTo = rows.fieldByName('latitude') * (Math.PI / 180);
var longitudeTo = rows.fieldByName('longitude') * (Math.PI / 180);
var a = Math.pow(Math.sin((latitudeTo - latitudeFrom) / 2), 2) + (Math.cos(latitudeFrom) * Math.cos(latitudeTo) * Math.pow(Math.sin((longitudeTo - longitudeFrom) / 2), 2));
var placeDistance = (2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))) * earthRadius;
if(placeDistance <= distanceKM)
{
dataDistance.push(
{
id: rows.fieldByName('id'),
distance: placeDistance,
county: rows.fieldByName('county'),
type: rows.fieldByName('category')
});
}
rows.next();
Titanium.API.info(dataDistance);
}
rows.close();
db.close();
});
Have I got this all wrong? Please someone help me!
2 Answers
-
Accepted Answer
i create a datastorage object to store my lat/lon coords in when i start to work with geolocations. It goes like this:
function dataStorage() { this.lat; this.lon; } var g = new dataStorage();
then the geolocation part:
if (Titanium.Geolocation.locationServicesEnabled==false) { Titanium.UI.createAlertDialog({title:'Geotriggerd AV Streams', message:'Your device has geo turned off - turn it on.'}).show(); } else { if (Titanium.Geolocation.hasCompass) { Titanium.Geolocation.showCalibration = false; Titanium.Geolocation.headingFilter = 90; Ti.Geolocation.getCurrentHeading(function(e) { if (e.error) { currentHeading.text = 'error: ' + e.error return; } var x = e.heading.x; var y = e.heading.y; var z = e.heading.z; var magneticHeading = e.heading.magneticHeading; var accuracy = e.heading.accuracy; var trueHeading = e.heading.trueHeading; var timestamp = e.heading.timestamp; }); Titanium.Geolocation.addEventListener('heading',function(e) { if (e.error) { Titanium.API.info("error: " + e.error); return; } var x = e.heading.x; var y = e.heading.y; var z = e.heading.z; var magneticHeading = e.heading.magneticHeading; var accuracy = e.heading.accuracy; var trueHeading = e.heading.trueHeading; var timestamp = e.heading.timestamp; }); } else { Titanium.API.info("No Compass on device"); } Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST; Titanium.Geolocation.distanceFilter = 10; Titanium.Geolocation.getCurrentPosition(function(e) { if (e.error) { Titanium.API.info("error: " + JSON.stringify(e.error)); return; } var longitude = e.coords.longitude; var latitude = e.coords.latitude; var altitude = e.coords.altitude; var heading = e.coords.heading; var accuracy = e.coords.accuracy; var speed = e.coords.speed; var timestamp = e.coords.timestamp; var altitudeAccuracy = e.coords.altitudeAccuracy; g.lat = latitude; g.lon = longitude; }); Titanium.Geolocation.addEventListener('location',function(e) { if (e.error) { Titanium.API.info("error: " + JSON.stringify(e.error)); return; } var longitude = e.coords.longitude; var latitude = e.coords.latitude; var altitude = e.coords.altitude; var heading = e.coords.heading; var accuracy = e.coords.accuracy; var speed = e.coords.speed; var timestamp = e.coords.timestamp; var altitudeAccuracy = e.coords.altitudeAccuracy; g.lat = latitude; g.lon = longitude; }); }
now you can use the g.lat and g.lon variables for your db queries
-
The geo functions are async, which means they don't necessarily return data when you call them but will do so when they get around to it. The best thing to do is to have the geo functions set some global variables. Then, put your db code in a separate function, but have it check those global variables for reasonable values before running. You can wrap code in try/catch blocks to prevent app errors.