javascript - 提高 GPS 的准确性(在 HTML5 地理定位中)

标签 javascript google-maps gps

如何在此代码中提高 GPS 的准确性?

var map;

function initialize() {
  var mapOptions = {
    zoom: 18
  };
  map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);

  // Try HTML5 geolocation
  if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = new google.maps.LatLng(position.coords.latitude,
                                       position.coords.longitude);


      var marker = new google.maps.Marker({
          position: pos,
          map: map,
          title: 'You Are Here'

      });

      map.setCenter(pos);
    }, function() {
      handleNoGeolocation(true);
    });
  } else {
    // Browser doesn't support Geolocation
    handleNoGeolocation(false);
  }

}

有办法吗?我知道使用 EnableHighAccuracy: true 是可能的,但我不知道我把这个命令放在哪里。有人有解决办法吗?

最佳答案

引用表格 Geolocation API spec :

interface Geolocation { 
       void getCurrentPosition(PositionCallback successCallback,
                               optional PositionErrorCallback errorCallback,
                               optional PositionOptions options)
       [...]
  }

[...]

The enableHighAccuracy attribute provides a hint that the application would like to receive the best possible results.

这意味着您可以像这样将其作为最后一个参数传递给对象(最小示例):

navigator.geolocation.getCurrentPosition(
    function(position) { console.log(position); }, // Success callback
    function() {}, // Error callback
    {enableHighAccuracy: true} // Position Options
);

关于javascript - 提高 GPS 的准确性(在 HTML5 地理定位中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30804460/

相关文章:

javascript - Browserify 分离 libs.js 和 app.js 导致 Backbone 找不到 jQuery

java - Google Maps Android api v2 折线长度

jquery - 使用 fancybox 时谷歌地图在新窗口中显示图像的问题

android - iphone 类似 android 的位置标记

python - 为什么这个 Python 半正矢公式会产生错误的答案?

javascript - 在内容可编辑的 div 上调整字体大小

javascript - 将外部 jsx 文件添加到 html 页面( react )

javascript - 如何多次将新行 append 到表中?

javascript - 谷歌地图标记

android - 由于不包括大地水准面高度,Android 的 GPS 高度是否不正确?