Fra*_* VE 17 html javascript html5 geolocation google-maps-api-3
我想找到一个使用嵌入式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>
Run Code Online (Sandbox Code Playgroud)
Men*_*los 27
您需要一个PositionOptions
对象,在API中设置高精度标志.
我在这里引用:http://diveintohtml5.info/geolocation.html
getCurrentPosition()函数有一个可选的第三个参数,一个PositionOptions对象.您可以在PositionOptions对象中设置三个属性.所有属性都是可选的.您可以设置任何或全部或不设置它们.
POSITIONOPTIONS OBJECT
Property Type Default Notes
--------------------------------------------------------------
enableHighAccuracy Boolean false true might be slower
timeout long (no default) in milliseconds
maximumAge long 0 in milliseconds
Run Code Online (Sandbox Code Playgroud)
所以,它应该像这样工作:
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.");
}
Run Code Online (Sandbox Code Playgroud)
注意到我添加了以下2个参数来getCurrentPosition
调用:
function error(msg){alert('Please enable your GPS position future.');}
无法检索GPS或触发超时时调用此功能.
{maximumAge:10000, timeout:5000, enableHighAccuracy: true});
这些是选项.我们不希望gps数据超过10秒(maximumAge:10000
).我们不希望等待超过5秒的响应(timeout:5000
),我们希望启用高精度(enableHighAccuracy: true
).
另请参阅:地理定位HTML5 enableHighAccuracy True,False还是Best Option?
这是enableHighAccuracy: true
取自Mozilla Developer Network的简单示例。
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);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
41184 次 |
最近记录: |