0

I am using phonegap, jquerymobile the googlemap API to get my current position and to watch my position.

For this, when I lunch my map page, my position is shown with a marker and the marker move when I move. Even if it works excpeted when I close my App (onPause).

Here is my code (you can tell me how I can perfect it :o) )

$('#home').live("pagebeforeshow", function() {

    if($('#googleAPI').length != 0){


        navigator.geolocation.getCurrentPosition(function(position){

            //showMap('mapHome',position.coords.latitude, position.coords.longitude);// Canvas, lat, long

            var latLng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);

            // Google Map options
            var myOptions = {
                zoom: 17,
                //zoomControl   : 1,
                center: latLng,
                mapTypeId: google.maps.MapTypeId.ROADMAP////ROADMAP, SATELLITE, HYBRID and TERRAIN
            };      

            // Create the Google Map, set options
            Tracking.mapy = new google.maps.Map(document.getElementById('mapHome'), myOptions);

            //addMarker(position.coords.latitude,position.coords.longitude);

        }, 
        showError, 
        {

                enableHighAccuracy  : true,
                maximumAge          : 2000
                //maximumAge:Infinity
        });

    }
})  

    $('#home').live("pageshow", function() {

        // Place and move the marker regarding to my position and deplacement
    if($('#googleAPI').length != 0){

        //var track_id = "me";
        Tracking.watch_id = navigator.geolocation.watchPosition(
        // Success
        function(position){

            var lat = position.coords.latitude;
            var long = position.coords.longitude;

            var latLng = new Array();
            latLng[0] = lat;
            latLng[1] = long;

            //Tracking.myCoordinates.push(lat,long);
            Tracking.myCoordinates.push(latLng);

            addMarker(lat, long);

        },
        // Error
        showError,
        { 
            frequency: 1000

        });

        console.log('HW : WatchPosition called. Id:' + Tracking.watch_id);


    }else{
        Alert.show('The map API has not been loaded. Check for connection and try again. (pagebeforeshow)');
    }


})

$('#home').live("pagebeforehide", function() {

    if($('#googleAPI').length != 0){

        //track_id = "me";

        // Stop tracking the user

        if (Tracking.watch_id != null) {
            navigator.geolocation.clearWatch(Tracking.watch_id);
        console.log('HW : WatchPosition cancelled Id:' + Tracking.watch_id);
            Tracking.watch_id = null;
        }


        //navigator.geolocation.clearWatch(Tracking.watch_id);



        //Tracking.watch_id = null;
        Tracking.myCoordinates = new Array();

    }else{
        Alert.show('The map API has not been loaded. Check for connection and try again. (pagebeforeshide)');
    }

});

The problem is when I close my App, because I still need to be alert when I go outside of my geofence. Then, as lomg as I do not stop to watch my position, I need it to be watching even if I close my position or if I lunch another app.

Then I do not know how to do when I called that Phonegap even:

document.addEventListener('pause', onPause, false);
function onPause(){}

Should I simply relunch my watch code with a different watch_id?

Any suggestion?

Many thank and happy new year

pierrot10
  • 129
  • 4
  • 13
  • Also, when the App is watching my position, I am looking to get a message to be alerted when the GPS signal is lost. I know that the event avigator.geolocation.watchPosition has an error event. But right now, I lunch the map page. and then I unactivate the GPS, on my smartphone, and no error message is display due to the showError() function. It seams to be logic because as the GPS is not available, it can not return the new position even if I seted up the frequency at 1sec. Is'nt right? Is there a listner method which check the availabilty of the GPS? Thanks – pierrot10 Jan 01 '13 at 20:15

1 Answers1

0

I think what you're trying to get at is running a background process using phonegap. The discussion in this link seems to say that it's only possible if you write a plugin to perform that functionality. PhoneGap doesn't have an API to do it out of the box.

Executing javascript in background using phonegap

Community
  • 1
  • 1
Nick Roth
  • 3,057
  • 2
  • 15
  • 15