【问题标题】:HTML 5 Geolocation watchPositionHTML 5 地理位置 watchPosition
【发布时间】:2011-12-02 15:14:30
【问题描述】:

我正在尝试设置我的代码以在 Android 手机或平板电脑上定位最准确的位置。由于 getCurrentPosition 没有给 GPS 足够的时间来寻找位置,我正在使用 watchPosition。这很好用,但我需要允许用户停止这个 watchPostion,所以我使用 clearWatch 功能。此 clearWatch 功能适用于我的 android 手机 2.2.2 版本,但不适用于 android 平板电脑 3.2.1 版本。我的另一个问题是在我的安卓手机上,一旦我停止/清除手表并尝试再次定位我的位置,我的手机就会振动并且浏览器关闭。这里有什么问题?我在其他手机上也试过了,也有同样的问题。如果有人有任何建议,我将不胜感激。下面是我正在使用的代码。

//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");
  }
}

【问题讨论】:

  • 为什么在执行 getCurrentPosition 时不设置一些大的超时时间?

标签: javascript html geolocation


【解决方案1】:

您的语法不正确。 clearWatch 接受要取消哪个手表 ID 的参数。

在你的情况下,你应该有clearWatch(watchID),而不是clearWatch()

【讨论】:

    猜你喜欢
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多