Titanium JIRA Archive
Appcelerator Community (AC)

[AC-2351] Start and finish Titanium.Geolocation.getCurrentPosition

GitHub Issuen/a
TypeBug
Priorityn/a
StatusClosed
ResolutionFixed
Resolution Date2014-01-16T05:28:24.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsAppcelerator Modules, Titanium SDK & CLI
Labelsandroid
ReporterMarian Bresol
AssigneeShak Hossain
Created2013-11-28T17:38:02.000+0000
Updated2016-03-08T07:41:22.000+0000

Description

My application use the the device's gps, and it works of this way, I make a function where {noformat} Titanium.Geolocation.getCurrentPosition {noformat} is called in different parts of my app.
exports.currentPosition = function() {
	try {
		Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST;
		Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS;
		Titanium.Geolocation.distanceFilter = 10;
		if (Titanium.Geolocation.locationServicesEnabled === false) {
			alert('Your device has GPS turned off. Please turn it on.');
			return {
				'longitude' : 0,
				'latitude' : 0,
				'altitude' : 0,
				'heading' : 0,
				'accuracy' : 0,
				'speed' : 0,
				'timestamp' : 0,
				'altitudeAccuracy' : 0
			};
		}
		var longitude, latitude, altitude, heading, accuracy, speed, timestamp, altitudeAccuracy;
		Titanium.Geolocation.getCurrentPosition(function(e) {
			if (!e.success || e.error) {
				var msg = L('msgUnableLocation');
				Titanium.UI.createAlertDialog({
					title : L('titleAlertValidation'),
					message : String(msg),
					buttonNames : [L('btnDone')]
				}).show();
				return {
					'longitude' : 0,
					'latitude' : 0,
					'altitude' : 0,
					'heading' : 0,
					'accuracy' : 0,
					'speed' : 0,
					'timestamp' : 0,
					'altitudeAccuracy' : 0
				};
			}
			longitude = e.coords.longitude;
			latitude = e.coords.latitude;
			altitude = e.coords.altitude;
			heading = e.coords.heading;
			accuracy = e.coords.accuracy;
			speed = e.coords.speed;
			timestamp = e.coords.timestamp;
			altitudeAccuracy = e.coords.altitudeAccuracy;
		});
		return {
			'longitude' : longitude,
			'latitude' : latitude,
			'altitude' : altitude,
			'heading' : heading,
			'accuracy' : accuracy,
			'speed' : speed,
			'timestamp' : timestamp,
			'altitudeAccuracy' : altitudeAccuracy
		};
		longitude = null;
		latitude = null;
		altitude = null;
		heading = null;
		accuracy = null;
		speed = null;
		timestamp = null;
		altitudeAccuracy = null;
	} catch( s_error ) {
		Titanium.UI.createAlertDialog({
			title : "fields gps",
			message : String(s_error),
			buttonNames : ['OK']
		}).show();
	}
}; 
And then this function is called in a different place this way:
	var core=  require('modules/core');

	formGeographic.addEventListener('maxGeoButton', function(e) {
		maxAltitudePosition = core.currentPosition();
		var fieldRefs = formGeographic.fieldRefs;
	    fieldRefs['maxGeoButton'].setTitle(L('labelGPSPointWasTakenMax'));
	}); 

How can finish this function? because when I save the register, the application crashed, yes save the register but my app rebooted.

Comments

  1. Motiur Rahman 2013-12-07

    Hi Marian, Your exports function looks good. But we are missing some pieces of information that make it difficult for us to reproduce it. For reproducing this issue please provide us a full test case and steps to reproduce. Thanks
  2. Mauro Parra-Miranda 2014-01-10

    Hello Marian,

    Introduction

    I checked on your code, and created a simple test case for this. In the testing of the testcase, I have been calling a similar function for 4500 times, without crashing so far.

    Test Case

    This testcase is in two files. App.js that contains the main file calling the commonjs module and the commonjs module. Both files will be in the Resources directory.

    app.js

       var geo = require('geo');
       
       var win = Ti.UI.createWindow({
       	backgroundColor:'white',
       });
       var i =0; 
       setInterval(function(){
       			Ti.API.info("Function calls without crashing: "+i++);
       			geo.currentPosition();
       			
       },1000);
       
       win.open();
       

    geo.js

       var init = false;
       
       function initGeo() {
       	if (init == false) {
       		Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;
       		Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS;
       		Ti.Geolocation.distanceFilter = 10;
       		if (Titanium.Geolocation.locationServicesEnabled === false) {
       			alert('Your device has GPS turned off. Please turn it on.');
       			return;
       		}
       		init = true;
       	}
       }
       
       //any property attached to the exports object becomes part of the module's public interface
       exports.currentPosition = function() {
       	initGeo();
       	Titanium.Geolocation.getCurrentPosition(function(e) {
       		if (!e.success || e.error) {
       			alert("Can't get the current position");
       			return {
       				'longitude' : 0,
       				'latitude' : 0,
       				'altitude' : 0,
       				'heading' : 0,
       				'accuracy' : 0,
       				'speed' : 0,
       				'timestamp' : 0,
       				'altitudeAccuracy' : 0
       			};
       		} else {
       			//alert("lon " + e.coords.longitude + " lat " + e.coords.latitude);
       			Ti.API.info("lon " + e.coords.longitude + " lat " + e.coords.latitude)
       			return {
       				'longitude' : e.coords.longitude,
       				'latitude' : e.coords.latitude,
       				'altitude' : e.coords.altitude,
       				'heading' : e.coords.heading,
       				'accuracy' : e.coords.accuracy,
       				'speed' : e.coords.speed,
       				'timestamp' : e.coords.timestamp,
       				'altitudeAccuracy' : e.coords.altitudeAccuracy,
       			};
       		}
       	});
       };
       

    Testing environment

    Mobile SDK 3.2.0 Android 4.4 Kitkat Nexus 4

    Steps to test

    1. Create a new mobile project (classic titanium with 3.2.0.GA Mobile SDK) 2. Paste the testcase in app.js and geo.js in the Resources directory. 3. Run in device 4. Check in the terminal for the number of times that the test has been run.

    Analysis of the code

    In your original code you are setting up the GPS in every single call. With the commonjs you can put the configuration as internal function (only doing something if is the first time you are configuring the commonjs module, doing nothing otherwise) and the shared function similar to yours. Your crashing might be related to configuring the GPS several times in short time.

    Recommendations

    Please modify the pattern on your commonjs module to mimic the init function, and then let us know if that helped you. Best, Mauro
  3. Shak Hossain 2014-01-16

    Marking this as resolved since the issue cannot be reproduced. We posted a functional sample that was tested extensibly to reproduce the crash scenario.

JSON Source