【问题标题】:High accuracy geolocation Html5高精度地理定位Html5
【发布时间】:2013-04-18 14:23:21
【问题描述】:

我正在尝试使用嵌入式 GPS 定位设备(例如应用程序共享位置)。我读过enableHighAccuracy: true 是可能的。

如何在这段代码中设置enableHighAccuracy: true?我尝试了不同的位置,但它不起作用。

<script type="text/javascript">
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            var latitude = position.coords.latitude;
            var longitude = position.coords.longitude;
            var accuracy = position.coords.accuracy;
            var coords = new google.maps.LatLng(latitude, longitude);
            var mapOptions = {
                zoom: 15,
                center: coords,
                mapTypeControl: true,
                navigationControlOptions: {
                    style: google.maps.NavigationControlStyle.SMALL
                },
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

            var capa = document.getElementById("capa");
            capa.innerHTML = "latitude: " + latitude + ", longitude: " + ", accuracy: " + accuracy;  

            map = new google.maps.Map(document.getElementById("mapContainer"), mapOptions);
            var marker = new google.maps.Marker({
                position: coords,
                map: map,
                title: "ok"
            });
        });

    } else {
        alert("Geolocation API is not supported in your browser.");
    }

</script>

【问题讨论】:

    标签: javascript html google-maps-api-3 geolocation


    【解决方案1】:

    您需要一个 PositionOptions 对象,您可以在其中按照 API 设置高精度标志。

    我从这里引用:http://diveintohtml5.info/geolocation.html

    getCurrentPosition() 函数有一个可选的第三个参数,a 位置选项对象。您可以在其中设置三个属性 位置选项对象。所有属性都是可选的。你可以设置 任何或全部或没有。

    POSITIONOPTIONS OBJECT
    
    Property            Type        Default         Notes
    --------------------------------------------------------------
    enableHighAccuracy  Boolean     false           true might be slower
    timeout             long        (no default)    in milliseconds
    maximumAge          long        0               in milliseconds
    

    所以,它应该像这样工作:

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            var latitude = position.coords.latitude;
            var longitude = position.coords.longitude;
            var accuracy = position.coords.accuracy;
            var coords = new google.maps.LatLng(latitude, longitude);
            var mapOptions = {
                zoom: 15,
                center: coords,
                mapTypeControl: true,
                navigationControlOptions: {
                    style: google.maps.NavigationControlStyle.SMALL
                },
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
    
            var capa = document.getElementById("capa");
            capa.innerHTML = "latitude: " + latitude + ", longitude: " + ", accuracy: " + accuracy;
    
            map = new google.maps.Map(document.getElementById("mapContainer"), mapOptions);
            var marker = new google.maps.Marker({
                position: coords,
                map: map,
                title: "ok"
            });
    
        },
        function error(msg) {alert('Please enable your GPS position feature.');},
        {maximumAge:10000, timeout:5000, enableHighAccuracy: true});
    } else {
        alert("Geolocation API is not supported in your browser.");
    }
    

    注意到我在 getCurrentPosition 调用中添加了以下 2 个参数:

    1. function error(msg){alert('Please enable your GPS position future.');}

      当无法获取 GPS 或触发超时时调用此函数。

    2. {maximumAge:10000, timeout:5000, enableHighAccuracy: true});

      这些是选项。我们不希望超过 10 秒的 gps 数据 (maximumAge:10000)。我们不想等待超过 5 秒的响应 (timeout:5000),我们希望启用高精度 (enableHighAccuracy: true)。

    另见:Geolocation HTML5 enableHighAccuracy True , False or Best Option?

    【讨论】:

    • 谢谢!!但是,我的精度不能低于 65 米 :(
    • @meework 您已将 enableHighAccuracy 设置为 false 而不是将其设置为 true。
    【解决方案2】:

    这是一个来自Mozilla Developer NetworkenableHighAccuracy: true 的简单示例。

    var options = {
      enableHighAccuracy: true,
      timeout: 5000,
      maximumAge: 0
    };
    
    function success(pos) {
      var crd = pos.coords;
    
      console.log('Your current position is:');
      console.log(`Latitude : ${crd.latitude}`);
      console.log(`Longitude: ${crd.longitude}`);
      console.log(`More or less ${crd.accuracy} meters.`);
    }
    
    function error(err) {
      console.warn(`ERROR(${err.code}): ${err.message}`);
    }
    
    navigator.geolocation.getCurrentPosition(success, error, options);
    

    【讨论】:

      【解决方案3】:
      if(navigator.geolocation) {
         navigator.geolocation.getCurrentPosition(position => {
             do_whatever_you_want
      },
      error => console.log(error),
      {enableHighAccuracy: true}
      )
      }
      

      解释: 首先,我正在检查浏览器是否支持使用顶部的if 条件并将整个代码包装在其中的地理位置。如果它存在,那么我尝试获取getCurrentPosition,它返回一个包含坐标等信息的对象。我称该对象为position。 所以如果你想得到latlng;

      var lt = position.coords.latitude;
      var ln = position.coords.longitude;
      

      GetCurrentPosition 还需要更多参数。第一个是如果它运行成功(然后我试图获取返回的对象)。第二个是error。我在这里控制台记录了错误。第三个是额外的选项。其中一个选项是enableHighAccuracy。如果设置为true,它将尝试从用户那里获取最准确的位置。

      【讨论】:

      • 请解释一下你的答案
      • 对我没用。显示相同的 presizenees,就好像我在没有 enableHighAccuracy: true expression 的情况下使用它一样
      • 您能否分享一下您正在使用的设备的详细信息。
      猜你喜欢
      • 1970-01-01
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      相关资源
      最近更新 更多