Search code examples
iphonehtmlgeolocationgpsgeo

HTML5 geolocation iphone


Heey People,

I've wrote something to find the gps location of my iphone. If my location is found it should show my location in an alertbox.

But apparently it keeps showing me the alertbox on my iphone over and over again.

I've wrote my class so that I can set an callback function. Because when you try to get your location it is not set till your found.

The callback function will be executed when your location is found.

Can anyone tell me how to stop the location tracker after I'm found?

My class:

GPS = function() {
this.constructor();

}

var gpsLatitude = 0.0, gpsLongitude = 0.0, gpsCallback;

GPS.prototype.constructor = function(){ 
    this.getLocation();
}

 var afterNotification = function(){
    //Do something
};

GPS.prototype.setCallback = function(myfunction){ 
    gpsCallback = myfunction;
}

GPS.prototype.getLocation = function(){
    if (navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(this.showPosition, this.showError);
    }
    else{
        Lungo.Notification.error(
                "Error",                      //Title
                "Your device doesnt support GPS",     //Description
                "cancel",                     //Icon
                7,                            //Time on screen
                afterNotification             //Callback function
            );
    }
  }

GPS.prototype.showPosition = function(position)
{
    gpsLatitude = position.coords.latitude;
    gpsLongitude = position.coords.longitude;
    gpsCallback();

}

GPS.prototype.getLatitude = function()
{
    return gpsLatitude; 
}

GPS.prototype.getLongitude = function()
{
    return gpsLongitude;    
}

GPS.prototype.showError = function(error)
  {
  switch(error.code) 
    {
    case error.PERMISSION_DENIED:
          Lungo.Notification.error(
                    "Error",                      //Title
                    "Gebruiker staat geolocatie niet toe",     //Description
                    "cancel",                     //Icon
                    7,                            //Time on screen
                    afterNotification             //Callback function
                );
      break;
    case error.POSITION_UNAVAILABLE:
          Lungo.Notification.error(
                    "Error",                      //Title
                    "Locatie niet beschikbaar",     //Description
                    "cancel",                     //Icon
                    7,                            //Time on screen
                    afterNotification             //Callback function
                );
      break;
    case error.TIMEOUT:
        Lungo.Notification.error(
                "Error",                      //Title
                "Locatie timeout",     //Description
                "cancel",                     //Icon
                7,                            //Time on screen
                afterNotification             //Callback function
            );
      break;
    case error.UNKNOWN_ERROR:
        Lungo.Notification.error(
                "Error",                      //Title
                "Unkown location error",     //Description
                "cancel",                     //Icon
                7,                            //Time on screen
                afterNotification             //Callback function
            );
      break;
    }
  }

var GPS = new GPS();

The way I use the class:

var callback = function()
            {
                alert(GPS.getLongitude() + ", " + GPS.getLatitude());
            }
            GPS.setCallback(callback);

Solution

  • I,ve found my problem, I requested my location again in the getLattitude method. After removing this is works perfect.