2011-12-02 129 views
0

我正在嘗試設置我的代碼,以便在Android手機或平板電腦上找到最準確的位置。由於getCurrentPosition沒有給GPS足夠的時間來找到一個位置,我正在使用watchPosition。這很好,但我需要允許用戶停止這個watchPostion,所以我使用clearWatch函數。這clearWatch功能適用於我的Android手機2.2.2版本,但不適用於Android平板電腦3.2.1版本。我的其他問題是在我的android手機上,一旦我停止/清除表格,並嘗試重新找到我的位置,我的手機就會振動並關閉瀏覽器。這裏有什麼問題?我在其他手機上也嘗試過這種方法,並且遇到同樣的問題。如果有人有任何建議,我將不勝感激。以下是我正在使用的代碼。HTML 5地理位置手錶位置

//function to locate using GPS 
function ShowMyLocation(){ 
    if (navigator.geolocation) { 
    ShowProgressIndicator('map'); 
    watchID = navigator.geolocation.watchPosition(function(position){ 
     var mapPoint = esri.geometry.geographicToWebMercator(new esri.geometry.Point(position.coords.longitude, position.coords.latitude, new esri.SpatialReference({ 
     wkid: 4326 
     }))); 
     var graphicCollection = esri.geometry.geographicToWebMercator(new esri.geometry.Multipoint(new esri.SpatialReference({ 
     wkid: 4326 
     }))); 
     graphicCollection.addPoint(mapPoint); 
     geometryService.project([graphicCollection], map.spatialReference, function(newPointCollection){ 
     HideProgressIndicator(); 
     if (!map.getLayer(baseMapLayerCollection[0].Key).fullExtent.contains(mapPoint)) { 
      alert('Data not available for the specified address.'); 
      return; 
     } 
     mapPoint = newPointCollection[0].getPoint(0); 
     AddServiceRequest(mapPoint); 
     }); 
    }, function(error){ 
     HideProgressIndicator(); 
     switch (error.code) { 
     case error.TIMEOUT: 
       alert('Timeout'); 
       break; 
     case error.POSITION_UNAVAILABLE: 
      alert('Position unavailable'); 
      break; 
     case error.PERMISSION_DENIED: 
      alert('Permission denied'); 
      break; 
     case error.UNKNOWN_ERROR: 
      alert('Unknown error'); 
      break; 
     } 
    }, { 
     timeout: 5000, 
     maximumAge: 90000, 
     enableHighAccuracy: true  
    }); 

    } 
} 
function clearWatch(){ 
    // Cancel the updates when the user clicks a button. 
    if (watchID > 0) { 
    navigator.geolocation.clearWatch(); 
    alert("Stop tracking location"); 
    } 
} 
+0

爲什麼在getCurrentPosition時不設置一些大的超時? – dmitry

回答

3

您的語法錯誤。 clearWatch需要取消哪個手錶ID的參數。

在你的情況,你應該有clearWatch(watchID),而不是clearWatch()