Appecelerator Geolocation with Titanium

Dolamite Jenkins
Dolamite Jenkins used Ask the Experts™
on
I Got the app to post to my server ??? what I though would be more difficult... I what I need is to have the app to show the Geolocation changes on the screen ... I need to post longitude and latitude to the screen... I'm also sending Altitude and speed to server ... I can not assign  the longitude and latitude to a variable ... I also thought it may be a lazy load so I move the Geolocation function  prior to the "createLabble" still error ... How can I get the e.coor.latitude to print to label

var lat = Ti.UI.createLabel({
		top : 215,
		left : 40,
		text : latitude,
		color : "black",
		font : {
			fontSize : 10
		}
	});
	var Long = Ti.UI.createLabel({
		top : 215,
		left : 200,
		text : "longitude",
		color : "black",
		font : {
			fontSize : 10
		}
	});

Open in new window

function getLocation() {
	Ti.Geolocation.getCurrentPosition(function(e) {
		console.log(e);
		var latitude = e.coord.latitude;
	    var longitude = e.coord.longitude;
		
	});
}

Open in new window

Get this error
Script Error {
[ERROR] :      column = 19;
[ERROR] :      line = 32;
[ERROR] :      message = "Can't find variable: latitude";
[ERROR] :      sourceURL = "file:///Users/clayrichmond/Library/Developer/CoreSimulator/Devices/2185D8C3-E658-43EC-AE86-9AB1558DD5DE/data/Containers/Bundle/Application/AA37B7DA-55C2-47E1-8C23-6AF9F9E2FECB/tryAgain.app/app.js";
[ERROR] :      stack = "    at createTab(/app.js:32:19)\n    at (/app.js:9:26)\n    at global code(/app.js:168:70)\n    at require@[native code]\n    at require(/ti.internal/extensions/binding.js:21:25)\n    at (/ti.main.js:40:10)\n    at loadAsync(/ti.internal/bootstrap.loader.js:108:13)\n    at global code(/ti.main.js:37:52)";
[ERROR] :      toJSON = "<KrollCallback: 0x600002fb4740>";
[ERROR] :  }
[ERROR] :  Script Error {
[ERROR] :      column = 25;
[ERROR] :      line = 21;
[ERROR] :      message = "Module \"app.js\" failed to leave a valid exports object";
[ERROR] :      sourceURL = "file:///Users/clayrichmond/Library/Developer/CoreSimulator/Devices/2185D8C3-E658-43EC-AE86-9AB1558DD5DE/data/Containers/Bundle/Application/AA37B7DA-55C2-47E1-8C23-6AF9F9E2FECB/tryAgain.app/ti.internal/extensions/binding.js";
[ERROR] :      stack = "    at require@[native code]\n    at require(/ti.internal/extensions/binding.js:21:25)\n    at (/ti.main.js:40:10)\n    at loadAsync(/ti.internal/bootstrap.loader.js:108:13)\n    at global code(/ti.main.js:37:52)";
[ERROR] :      toJSON = "<KrollCallback: 0x600002fa3740>";
[ERROR] :  }

Open in new window


 Application booted in 870.676994 ms
[INFO] :   {
[INFO] :       accuracy = 5;
[INFO] :       altitude = 0;
[INFO] :       altitudeAccuracy = "-1";
[INFO] :       floor =     {
[INFO] :           level = 0;
[INFO] :       };
[INFO] :       heading = "-1";
[INFO] :       latitude = "37.78583526611328";
[INFO] :       longitude = "-122.4064178466797";
[INFO] :       speed = "-1";
[INFO] :       timestamp = 1561618818501;
[INFO] :   }
[INFO] :   {
[INFO] :       code = 0;
[INFO] :       coords =     {
[INFO] :           accuracy = 5;
[INFO] :           altitude = 0;
[INFO] :           altitudeAccuracy = "-1";
[INFO] :           floor =         {
[INFO] :               level = 0;
[INFO] :           };
[INFO] :           heading = "-1";
[INFO] :           latitude = "37.78583526611328";
[INFO] :           longitude = "-122.4064178466797";
[INFO] :           speed = "-1";
[INFO] :           timestamp = 1561618818501;
[INFO] :       };
[INFO] :       source = "[object GeolocationModule]";
[INFO] :       success = 1;
[INFO] :       type = location;
[INFO] :   }
[INFO] :   {
[INFO] :       accuracy = 5;
[INFO] :       altitude = 0;
[INFO] :       altitudeAccuracy = "-1";
[INFO] :       floor =     {
[INFO] :           level = 0;
[INFO] :       };
[INFO] :       heading = "-1";
[INFO] :       latitude = "37.78583526611328";
[INFO] :       longitude = "-122.4064178466797";
[INFO] :       speed = "-1";
[INFO] :       timestamp = 1561619327191;
[INFO] :   }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The first error is caused by what must be a typo,
text : latitude,

Open in new window

should be
text : "latitude",

Open in new window

Top Expert 2016

Commented:
I can not assign  the longitude and latitude to a variable . Why not

Author

Commented:
Have managed to get the GPS coordinate to output to the label widget, BUT I am tracking moving moving  objects , so I need the numbers to keep changing on the screen .... the program simulator is sending the data but the label never changes ... do I have to clear the table some how of shouldn't the new dat overwrite the old data ... see code below


function getLocation(latitude,longitude ) {
	Ti.Geolocation.getCurrentPosition(function(e) {
		console.log(e);
		var latitude = e.coords.latitude;
		var  longitude = e.coords.longitude;
		var Long = Ti.UI.createLabel({
		top : 215,
		left : 200,
		text : latitude,
		color : "black",
		font : {
			fontSize : 15
		}
	});
	var lat = Ti.UI.createLabel({
		top : 215,
		left : 40,
		text : longitude,
		color : "black",
		font : {
			fontSize : 15
		}
	});
	win.add(Long);
	win.add(lat);
		var xhr = Ti.Network.createHTTPClient();

		xhr.onload = function(e) {
			// handle response, which at minimum will be an HTTP status code
		};

		xhr.open('POST', 'http://www.semsreports.net/post.php');
		xhr.send({
			
			latitude : "e.coord.latitude",
	        longitude : "e.coord.longitude",
	        speed : "e.coor.speed",
	        timestamp : "e.coor.timestamp" 
		});
		
		//Here you can make a call to the server side and send the params you want
	});
}

getLocation();

	

	var username = Ti.UI.createTextField({
		backgroundColor : 'black',
		color : 'green',
		top : 20,
		width : 250,
		height : 40
	});
	var password = Ti.UI.createTextField({
		backgroundColor : 'black',
		color : 'green',
		top : 100,
		width : 250,
		height : 40
	});

	var Ebutton = Titanium.UI.createButton({
		title : 'Emergency',
		backgroundColor : "red",
		top : 350,
		width : 500,
		height : 50
	});
	Ebutton.addEventListener('click', function(f) {
		Titanium.API.info("You clicked the button");
	});

	win.add(username);
	win.add(password);
	
	
	win.add(Ebutton);
	var tab = Ti.UI.createTab({
		title : title,
		icon : icon,
		window : win
	});

	return tab;
}

if (Ti.Geolocation.locationServicesEnabled) {
	Ti.Geolocation.purpose = 'Get Current Location';
	Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;
	Ti.Geolocation.distanceFilter = 10;
	Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS;

	Ti.Geolocation.addEventListener('location', function(e) {
		if (e.error) {
			alert('Error: ' + e.error);
		} else {
			Ti.API.info(e.coords);
		}
	});
} else {
	alert('Please enable location services');
}

function getLocation() {
	Ti.Geolocation.getCurrentPosition(function(e) {
		console.log(e);
		
	});
}

getLocation();

Open in new window


Test Data below


coords =     {
[INFO] :           accuracy = 30;
[INFO] :           altitude = 0;
[INFO] :           altitudeAccuracy = "-1";
[INFO] :           floor =         {
[INFO] :               level = 0;
[INFO] :           };
[INFO] :         
[INFO] :           latitude = "37.33115768432617";
[INFO] :           longitude = "-122.0307693481445";
[INFO] :           
[INFO] :     
[INFO] :       heading = "103.2799987792969";
[INFO] :       latitude = "37.33045959472656";
[INFO] :       longitude = "-122.0303649902344";
[INFO] :       speed = "5.800000190734863";
[INFO] :       timestamp = 1561752215361;
[INFO] :   }
[INFO] :  
[INFO] :       heading = "88.33000183105469";
[INFO] :       latitude = "37.3304443359375";
[INFO] :       longitude = "-122.0297775268555";
[INFO] :       speed = "7.059999942779541";
[INFO] :       timestamp = 1561752223365;
[INFO] :   }
[INFO] :   {
[INFO] :       accuracy = 10;
[INFO] :       altitude = 0;
[INFO] :       altitudeAccuracy = "-1";
[INFO] :       floor =     {
[INFO] :           level = 0;
[INFO] :       };
[INFO] :       heading = "89.54000091552734";
[INFO] :       latitude = "37.33045196533203";
[INFO] :       longitude = "-122.0296173095703";
[INFO] :       speed = "7.070000171661377";
[INFO] :       timestamp = 1561752225360;
[INFO] :   }
[INFO] :   {
[INFO] :       accuracy = 10;
[INFO] :       altitude = 0;
[INFO] :       altitudeAccuracy = "-1";
[INFO] :       floor =     {
[INFO] :           level = 0;
[INFO] :       };
[INFO] :       heading = "91.90000152587891";
[INFO] :       latitude = "37.33045196533203";
[INFO] :       longitude = "-122.0294494628906";
[INFO] :       speed = "7.170000076293945";
[INFO] :       timestamp = 1561752227362;
[INFO] :   }
[INFO] :   {
[INFO] :       accuracy = 5;
[INFO] :       altitude = 0;
[INFO] :       altitudeAccuracy = "-1";
[INFO] :       floor =     {
[INFO] :           level = 0;
[INFO] :       };
[INFO] :       heading = "120.2200012207031";
[INFO] :       latitude = "37.33039474487305";
[INFO] :       longitude = "-122.0293045043945";
[INFO] :       speed = "7.21999979019165";
[INFO] :       timestamp = 1561752229367;
[INFO] :   }
[INFO] :   {
[INFO] :       accuracy = 10;
[INFO] :       altitude = 0;
[INFO] :       altitudeAccuracy = "-1";
[INFO] :       floor =     {
[INFO] :           level = 0;
[INFO] :       };
[INFO] :       heading = "116.9800033569336";
[INFO] :       latitude = "37.33034133911133";
[INFO] :       longitude = "-122.0291595458984";
[INFO] :       speed = "7.019999980926514";
[INFO] :       timestamp = 1561752231361;
[INFO] :   }
[INFO] :   {
[INFO] :       accuracy = 10;
[INFO] :       altitude = 0;
[INFO] :       altitudeAccuracy = "-1";
[INFO] :       floor =     {
[INFO] :           level = 0;
[INFO] :       };
[INFO] :       heading = "101.9700012207031";
[INFO] :       latitude = "37.33030700683594";
[INFO] :       longitude = "-122.0290145874023";
[INFO] :       speed = "6.849999904632568";
[INFO] :       timestamp = 1561752233353;
[INFO] :   }
[INFO] :   {
[INFO] :       accuracy = 5;
[INFO] :       altitude = 0;
[INFO] :       altitudeAccuracy = "-1";
[INFO] :       floor =     {
[INFO] :           level = 0;
[INFO] :       };
[INFO] :       heading = "116.2399978637695";
[INFO] :       latitude = "37.33024597167969";
[INFO] :       longitude = "-122.0288772583008";
[INFO] :       speed = "5.28000020980835";
[INFO] :       timestamp = 1561752236368;
[INFO] :   }
[INFO] :   {
[INFO] :       accuracy = 5;
[INFO] :       altitude = 0;
[INFO] :       altitudeAccuracy = "-1";
[INFO] :       floor =     {
[INFO] :           level = 0;
[INFO] :       };
[INFO] :       heading = "91.97000122070312";
[INFO] :       latitude = "37.33023834228516";
[INFO] :       longitude = "-122.0287628173828";
[INFO] :       speed = "5.21999979019165";
[INFO] :       timestamp = 1561752238382;
[INFO] :   }
[INFO] :   {
[INFO] :          {
[INFO] :           level = 0;
[INFO] :       };
[INFO] :       heading = "88.33999633789062";
[INFO] :       latitude = "37.33024215698242";
[INFO] :       longitude = "-122.0286407470703";
[INFO] :       speed = "5.269999980926514";
[INFO] :       timestamp = 1561752240367;
[INFO] :   }
[INFO] :   
[INFO] :       
[INFO] :       };
[INFO] :       heading = "91.01000213623047";
[INFO] :       latitude = "37.33024215698242";
[INFO] :       longitude = "-122.0285186767578";
[INFO] :       speed = "6.21999979019165";
[INFO] :       timestamp = 1561752242369;
[INFO] :   }
[INFO] :       };
[INFO] :       heading = "94.97000122070312";
[INFO] :       latitude = "37.33023071289062";
[INFO] :       longitude = "-122.0283737182617";
[INFO] :       speed = "6.710000038146973";
[INFO] :       timestamp = 1561752244354;
[INFO] :   }
[INFO] : ;
[INFO] :       };
[INFO] :       heading = "92.08000183105469";
[INFO] :       latitude = "37.33022689819336";
[INFO] :       longitude = "-122.0282211303711";
[INFO] :       speed = "6.679999828338623";
[INFO] :       timestamp = 1561752246379;
[INFO] :   }
[INFO] :  
[INFO] :           level = 0;
[INFO] :       };
[INFO] :       heading = "96.22000122070312";
[INFO] :       latitude = "37.33021545410156";
[INFO] :       longitude = "-122.0280685424805";
[INFO] :       speed = "7.03000020980835";
[INFO] :       timestamp = 1561752248393;

Open in new window

Top Expert 2016

Commented:
Where are you reading the data and how often are you calling get location? is it feeding into a queue and you are reading until the queue is empty? if the queue is >=1 is this when you are calling the get and display location?

pseudo code

do {
sleep 100  //pause 100 ms
get-location  //query gps location
display-location
whlle ( 0 == 0)  //infinite loop

Author

Commented:
I'm using the built in function that registers when the device moves to save battery life... I still cane get it to display on the screen any thing except the first GPF coordinates but no problem sending the data to the server... is the another was other than label or a print label - drop ;able- print new label loop ?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial