错误处理不适用于Chrome中的HTML5地理位置 [英] Error handling not working with HTML5 geolocation in Chrome

查看:164
本文介绍了错误处理不适用于Chrome中的HTML5地理位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的地图,只要允许HTML5地理位置定位,就应该以用户的位置为中心加载.如果他们不选择让我们看到他们的位置,则有一个适当的功能,但是由于某种原因,它不起作用.

I have a simple map that should load centered around the user's location as long as they allow HTML5 geolocation. There's a function in place incase they don't choose to allow us to see their location, but for some reason it doesn't work.

代码如下:

 var x = document.getElementById("msg");
    function getLocation() {
        if (Modernizr.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition, showError);
        }
        else { x.innerHTML = "Geolocation is not supported by this browser."; }
    }
    function showPosition(position) {
        var mapOptions = {
            center: new google.maps.LatLng(position.coords.latitude, position.coords.longitude),
            zoom: 10,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("Map"), mapOptions);
        var acOptions = {
            types: ['establishment']
        };
        var autocomplete = new google.maps.places.Autocomplete(document.getElementById('autocomplete'), acOptions);
        autocomplete.bindTo('bounds', map);
        var infoWindow = new google.maps.InfoWindow();
        var marker = new google.maps.Marker({
            map: map
        });
        google.maps.event.addListener(autocomplete, 'place_changed', function () {
            infoWindow.close();
            var place = autocomplete.getPlace();
            if (place.geometry.viewport) {
                map.fitBounds(place.geometry.viewport);
            } else {
                map.setCenter(place.geometry.location);
                map.setZoom(17);
            }
            marker.setPosition(place.geometry.location);
            infoWindow.setContent('<div><strong>' + place.name + '</strong><br />');
            infoWindow.open(map, marker);
            google.maps.event.addListener(marker, 'click', function (e) {
                infoWindow.open(map, marker);
            });
        });
    }

    function showError(error) {
        switch (error.code) {
            case error.PERMISSION_DENIED:
                x.innerHTML = "User denied the request for Geolocation."
                break;
            case error.POSITION_UNAVAILABLE:
                x.innerHTML = "Location information is unavailable."
                break;
            case error.TIMEOUT:
                x.innerHTML = "The request to get user location timed out."
                break;
            default:
                x.innerHTML = "An unknown error occurred."
                break;
        }
    }
    google.maps.event.addDomListener(window, 'load', getLocation);

当然,主体中还有一个<p>元素,其ID为msg.

And of course a <p> element is in the body with an id of msg.

推荐答案

您可能会注意到它在FF中不会触发,因为他们认为不是现在"不是应该引发错误-请参阅此处(最后的响应是停止重新打开此错误,这是设计使然"):

Your'e probably noticing it doesn't fire in FF, because they see the "not now" as not something that should fire an error - see here (last response says "stop reopening this bug, it's by design that we do it that way"):

https://bugzilla.mozilla.org/show_bug.cgi?id=635175

但是

超时是navigator.geolocation.getCurrentPosition的options参数(可选的第三个参数)的可接受属性.因此,navigator.geolocation.getCurrentPosition(showPosition, showError, {timeout:8000});是8秒钟的不活动"后到达showError的方法-在Firefox中,这意味着没有明确回答是或从不回答".

Timeout is an acceptable property of the options argument (optional third argument) of navigator.geolocation.getCurrentPosition. So navigator.geolocation.getCurrentPosition(showPosition, showError, {timeout:8000}); is the way to get to showError after 8 seconds of "inactivity" - which in the case of firefox means "not answering explicitly yes or never".

PS-问与答:如果用户拒绝在Firefox中共享地理位置,功能永远不会失败

这篇关于错误处理不适用于Chrome中的HTML5地理位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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