navigator.geolocation getCurrentPosition未在Chrome Mobile中更新

tro*_*ers 7 mobile google-chrome geolocation page-refresh

我创建了一个专为智能手机设计的网站(可在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>
Run Code Online (Sandbox Code Playgroud)

我玩过maximumAge和timeout的各种值,但似乎没有任何东西影响相同的position.coords和position.time值.

我认为Chrome Mobile可能存在一个问题,但我不想在这个时候假设太多,只需要澄清我在代码中没有犯过类似muppet的错误.

非常感谢您提供的任何帮助.

更新:我想我应该说我已经在两台Android设备上测试了这个; HTC One X +和三星Galaxy Tab 7.7具有相同的效果.在两个股票浏览器上工作正常,并在两个Chrome上都不刷新位置.稍后将在Apple设备上测试:)

tro*_*ers 7

我从来没有深究过这个问题,但是我通过利用这个问题解决了这个问题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();
}
Run Code Online (Sandbox Code Playgroud)

我没有玩过"选项"值或此刻的超时延迟,但上面的代码在我尝试的每个平台上都带回了准确的定位信息.

希望这可以帮助有同样问题的人:)