提高 GPS 的准确性(在 HTML5 地理位置中)

Increase accuracy of GPS (in HTML5 geolocation)

本文关键字:HTML5 地理位置 GPS 准确性 提高      更新时间:2023-09-26

如何提高此代码中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是可能的:真的,但我不知道我把这个命令放在哪里。有人有解决方案吗?

引用表单 地理位置 API 规范:

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

[...]

"启用高精度"属性提供了一个提示,表明应用程序希望获得最佳结果。

这意味着您可以在对象中将其作为最后一个参数传递,如下所示(最小示例):

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