Behavioural Technology Group | BTek

Latest Blogs from BTek

Tracking movement in Sencha Touch with progress updates from a native process

I made a simple GPS tracking app while working out how to send progress updates to a Sencha Touch application from a native process with PhoneGap.The main trick is to return with a “no result” status from the execute function.


    public PluginResult execute(String action, JSONArray data, String callbackId)
        this.callbackId = callbackId;

	        final LocationManager locationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
	        ctx.runOnUiThread(new RunnableLocationListener(this, callbackId, locationManager, getBestProvider(locationManager)));
    	catch (Exception ex)
    		Log.e("BTek PhoneGap GPS Trakker", ex.getMessage());

        PluginResult result = new PluginResult(Status.NO_RESULT);
        return result;

And whenever a progress update is sent, the KeepCallback property on the result must be set to true.

    public void gotLocation (Location location)
        	if (location != null)
	            JSONObject geoPosition = new JSONObject();
	            geoPosition.put("latitude", location.getLatitude());
	            geoPosition.put("longitude", location.getLongitude());

	            PluginResult result = new PluginResult(Status.OK, geoPosition);
	            success(result, callbackId);
        catch (Exception ex)
            error(new PluginResult(Status.JSON_EXCEPTION, ex.getMessage()), callbackId);

The mapping code is relatively straight-forward if you know the Google Maps API. The map is centered on the new coordinates, a line is drawn to it from the last location and a marker is placed.


	function SuccessHandler (result) {

		// Center the map
		me.getComponent('map').setMapCenter({ latitude: result.latitude, longitude: result.longitude});

		if ( !== null && !== '')
			// Add a line to the map
			var flightPlanCoordinates = [
				new google.maps.LatLng(,,
				new google.maps.LatLng(result.latitude, result.longitude)
			var flightPath = new google.maps.Polyline({
				path: flightPlanCoordinates,
				strokeColor: "#4682b4",
				strokeOpacity: 0.8,
				strokeWeight: 2

			// Remove the old marker;

		// Add a new marker
		var position = new google.maps.LatLng (result.latitude, result.longitude); = new google.maps.Marker({
			position: position,
			title: result.latitude + ', ' + result.longitude,
			icon: '',
			map: me.getComponent('map').getMap()
		});'map').getMap()); = result.latitude; = result.longitude;

	function ErrorHandler (error) {
		alert("Native call failed: " + error);
	'BTekGPSPlugin', '', []);
}, this);

Download the APK: PhoneGap GPS Trakker or browse the source code

Social Share Counters

Posted in: mobile, mobile sensing, phonegap, sencha touch

Leave a Comment: (0) →

Leave a Comment