1
Heey People,HTML5地理位置iphone
我寫了一些東西來找到我的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);
嘗試navigator.geolocation.clearWatch(),一旦你得到你的第一個位置。 – Ares 2013-02-19 07:25:01
Thx的小費,但它沒有正常工作的原因是因爲我在getLattitude函數中犯了一個錯誤,請求它再次在那裏 – 2013-02-19 07:27:57