8

スマートフォン用に設計されたサイト ( http://dev.gkr33.comでアクセス可能) を作成し、navigator.geolocation API を使用して getCurrentPosition を介して位置を取得しようとしました。これは最初は機能しているように見えますが、ページを更新しようとすると、常に最後の GPS 位置が返されます。getCurrentPosition が返される時間を取得するページにデバッグ情報を追加しました。最初のポジショニングの後、常に同じ時間 (ミリ秒まで) が返されます。

これは Chrome Mobile でのみ発生するようです。標準の Android ブラウザーでサイトを閲覧すると、毎回問題なく動作します。

コードを以下に示します。

<script type="text/javascript">
    (function ($) {
    $(document).ready(function() {
        var options = { enableHighAccuracy: true, maximumAge: 0, timeout: 60000 };
        var position;

        // empty the current html elements, not strictly necessary but
        // I'm clutching at straws
        $('#debug-latlng').empty();
        $('#debug-time').empty();
        $('#debug-address').empty();

        // Let's try and find out where we are
        if(navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(gotPos, gotErr, options ); 
        } else {
            gotErr();
        }

        // We've got our position, let's show map and update user
        function gotPos(position) {
            var info;
            info = position.coords.latitude+','+position.coords.longitude;
            $('#debug-latlng').text(info);
            $('#debug-time').text(parseTimestamp(position.timestamp));

            // the following json call will translate the longitude and
            // latitude into an address (a wrapper for google's geocode call)

            $.getJSON('http://dev.gkr33.com/api.php', { req: "getLocationInfo", latlng: $('#debug-latlng').text() }, function(json) {
                $('#debug-address').text( json['results'][0]['formatted_address'] );
            });

            var myLatLng = new google.maps.LatLng( position.coords.latitude, position.coords.longitude );
            var mapOptions = {
                    zoom: 12,
                    center: myLatLng,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
            };      
            var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

            var marker = new google.maps.Marker({
                position: myLatLng, 
                title: 'You are here',
                animation: google.maps.Animation.DROP 
            });
            marker.setMap(map);
        } //gotPos


        // Trap a GPS error, log it to console and display on site
        function gotErr(error) {
            var errors = { 
                    1: 'Permission denied',
                    2: 'Position unavailable',
                    3: 'Request timeout'
                };
            console.log("Error: " + errors[error.code]);
            $('#debug-latlng').text('GPS position not available');
        } //gotErr

        // Make timestamp human readable
        function parseTimestamp(timestamp) {
            var d = new Date(timestamp);
            var day = d.getDate();
            var month = d.getMonth() + 1;
            var year = d.getFullYear();
            var hour = d.getHours();
            var mins = d.getMinutes();
            var secs = d.getSeconds();
            var msec = d.getMilliseconds();
            return day + "." + month + "." + year + " " + hour + ":" + mins + ":" + secs + "," + msec;
        } // parseTimestamp     
    });         
}) (jQuery);
</script>

maximumAge と timeout のさまざまな値を試してみましたが、同じ position.coords と position.time の値に影響を与えるものはないようです。

Chrome Mobile に問題がある可能性があると思いますが、現時点ではあまり想定したくありません。コードでマペットのようなプロポーションを間違えていないことを明確にする必要があります。

あなたが提供できる助けに感謝します。

更新: 2 つの Android デバイスでこれをテストしたと言うべきだったと思います。HTC One X+ と Samsung Galaxy Tab 7.7 で同じ結果が得られました。どちらの標準ブラウザでも問題なく動作し、どちらの Chrome でも位置が更新されません。後でAppleデバイスでテストします:)

4

3 に答える 3

8

私はこの問題の真相にたどり着くことはありませんでしたが、このwatchPosition呼び出しを利用して問題を回避し、これを 5 秒間待機してからクリアしましたwatchID。以下のコードを確認してください。

var options = { enableHighAccuracy: true, maximumAge: 100, timeout: 50000 };
if( navigator.geolocation) {
   var watchID = navigator.geolocation.watchPosition( gotPos, gotErr, options );
   var timeout = setTimeout( function() { navigator.geolocation.clearWatch( watchID ); }, 5000 );
} else {
   gotErr();
}

現時点では、「オプション」の値やタイムアウトの遅延をいじっていませんが、上記のコードは、試したすべてのプラットフォームで正確な位置情報を返します。

これが同じ問題を抱えている人に役立つことを願っています:)

于 2012-12-05T16:12:22.493 に答える