HTML5地理定位iphone



Heey People,

我写了一些东西来查找我的iphone的gps位置。如果找到我的位置,它应该在一个警告框中显示我的位置。

但很明显,它一次又一次地向我显示我iphone上的报警框。

我已经编写了我的类,以便可以设置回调函数。因为当你试图获得你的位置时,直到你找到它才被设置。

当找到您的位置时,将执行回调函数。

有人能告诉我在我被发现后如何停止位置跟踪器吗?

我的班级:

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();

我使用类的方式:

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

我发现了我的问题,我在getLattitude方法中再次请求了我的位置。去除后,这是完美的工作。

最新更新