24

組み込み GPS を使用してデバイスを見つけようとしています (whats app share location など)。で可能だと読みました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>
4

3 に答える 3

33

PositionOptionsAPI に従って高精度フラグを設定するオブジェクトが必要です。

ここから引用しています:http://diveintohtml5.info/geolocation.html

getCurrentPosition() 関数には、オプションの 3 番目の引数である PositionOptions オブジェクトがあります。PositionOptions オブジェクトで設定できるプロパティは 3 つあります。すべてのプロパティはオプションです。それらのいずれかまたはすべてを設定するか、どれも設定しないことができます。

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 つのパラメーターをgetCurrentPosition呼び出しに追加したことに気付きました。

  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?

于 2013-04-24T21:33:39.877 に答える
7

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);
于 2018-10-24T14:34:33.750 に答える