高精度地理定位Html5

问题描述 投票:16回答:2

我正在尝试使用嵌入式GPS定位设备(如应用程序共享位置)。我已经读过qazxsw poi有可能。

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

enableHighAccuracy: true
javascript html html5 google-maps-api-3 geolocation
2个回答
26
投票

你需要一个<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> 对象,你可以在API中设置高精度标志。

我在这里引用:PositionOptions

getCurrentPosition()函数有一个可选的第三个参数,一个PositionOptions对象。您可以在PositionOptions对象中设置三个属性。所有属性都是可选的。您可以设置任何或全部或不设置它们。

http://diveintohtml5.info/geolocation.html

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

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."); } 调用中添加了以下2个参数:

  1. getCurrentPosition 无法检索GPS或触发超时时调用此功能。
  2. function error(msg){alert('Please enable your GPS position future.');} 这些是选项。我们不希望gps数据超过10秒({maximumAge:10000, timeout:5000, enableHighAccuracy: true});)。我们不想等待超过5秒的响应(maximumAge:10000),我们希望启用高精度(timeout:5000)。

另见:enableHighAccuracy: true


1
投票

这是一个从Geolocation HTML5 enableHighAccuracy True , False or Best Option?取得的enableHighAccuracy: true的简单例子。

Mozilla Developer Network
© www.soinside.com 2019 - 2024. All rights reserved.