高精度的地理定位Html5 [英] High accuracy geolocation Html5

查看:121
本文介绍了高精度的地理定位Html5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找使用嵌入式GPS的设备(例如什么app共享位置)。我读过EnableHighAccuracy:true的情况。

I am tring to locate a device using the embedded GPS (like whats app share location). I've read that it is possible with EnableHighAccuracy: true.

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

How can I set "enableHighAccuracy: true" in this code? I tried in different positions but it doesn't work.

<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 = "latitud: " + latitude + " longitud: " + "   aquesta es la precisio en metres  :  " + 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>

非常感谢!

Thanks a lot!

推荐答案

您需要一个PositionOptions对象,您可以在该API后面设置高精度标志。

You need a PositionOptions object, in which you set the high accuracy flag following the API.

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


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

The getCurrentPosition() function has an optional third argument, a PositionOptions object. There are three properties you can set in a PositionOptions object. All the properties are optional. You can set any or all or none of them.

POSITIONOPTIONS OBJECT

POSITIONOPTIONS OBJECT

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

像这样:

So, it should work like this:

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 = "latitud: " + latitude + " longitud: " + "   aquesta es la precisio en metres  :  " + 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 future.');  

  }, {maximumAge:600000, timeout:5000, enableHighAccuracy: true});

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

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

Noticed I added the following 2 params to getCurrentPosition call:

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

, {maximumAge:600000, timeout:5000, enableHighAccuracy: true});

另请参阅:地理位置HTML5 enableHighAccuracy True,False或Best Option?

这篇关于高精度的地理定位Html5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆