“许可被拒绝”当试图获取位置 [英] "Permission Denied" when trying to fetch location

查看:95
本文介绍了“许可被拒绝”当试图获取位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 当我尝试使用此代码获取我的位置时,我总是收到Permission Denied > function initialize(){
$('。map-fullsize')。hide();
$('#weather-map')。hide();
$('#weather-data')。hide();

if(geo_position_js.init()){
var waiting_time = $('#getting-position')。html('Försökeratt hitta din aktuella position。Var godvänta... );

t = setTimeout(function(){
waiting_time.html('Det tarlängretid att hitta din position,änvad det egentligen bordegöra。< br>< br>< b>提示< / b>< br> GPS-mottagaren harlättareatt hitta dig om utärutomhus。Tätamoln som till exempel vid ettåskoväder,kangöradetsvårareförsatelliterna atthämtadin position。');
},60000);

geo_position_js.getCurrentPosition(show_position,function(){
clearTimeout(t);
$('#getting-position')。html('< b> Ett fel ');
},{
enableHighAccuracy:true
}< b>< br> );
} else {
$('#getting-position')。html('< b> Ett fel uppstod< / b>< br> 。');




$ b函数show_position(p){
$('。map-fullsize')。show();
$('#weather-map')。show();
$('#weather-data')。show();
$('#getting-position')。hide();


if(navigator.geolocation){

navigator.geolocation.getCurrentPosition(showError,function(position){
var latitude = position.coords .latitude;
var longitude = position.coords.longitude;
var accuracy = position.coords.accuracy;
var speed = position.coords.speed;
var altitude = position .coords.altitude;
var heading = position.coords.heading;

var coords = new google.maps.LatLng(latitude,longitude);
var mapOptions = {
center:coords,
streetViewControl:false,

mapTypeControl:true,
navigationControlOptions:{
style:google.maps.NavigationControlStyle.SMALL
},

zoomControl:true,
zoomControlOptions:{
style:google.maps.ZoomControlStyle.SMALL,
position:google.maps.ControlPosition.TOP_LEFT
},

mapTypeId:google.maps.MapTypeId.ROADMAP
};

map = new google.maps.Map(
document.getElementById('weather-map'),mapOptions
);

var marker = new google.maps.Marker({
position:coords,
map:map
});

var circle = new google.maps.Circle({
center:coords,
radius:准确,
map:map,
fillColor:' #3333ff',
fillOpacity:0.4,
strokeColor:'#3333ff',
strokeOpacity:0.8,
strokeWeight:1
});


map.setCenter(coords);

if(accuracy> 30){
map.fitBounds(circle.getBounds());
} else {
map.setZoom(14);

$ b $('#weather-data')。load('jquery-fetch / fetch-weatherdata.php?coor ='+ latitude.toFixed(6).replace(/ \。,'')+','+ longitude.toFixed(6).replace(/\./,'')+'& coordinates ='+ latitude.toFixed(6)+','+ longitude.toFixed(6)+'& accuracy ='+ accuracy +'& speed ='+ speed +'& altitude ='+ altitude +'& heading ='+ heading);
});

} else {
alert('Geolocation APIstödsinte i dinwebbläsare');
}



函数showError(错误){
switch(error.code){
case error.PERMISSION_DENIED:
$('。map-fullsize')。hide();
$('#weather-map')。hide();
$('#weather-data')。hide();
$('#permission-denied')。show();
休息;

case error.POSITION_UNAVAILABLE:
$('。map-fullsize')。hide();
$('#weather-map')。hide();
$('#weather-data')。hide();
$('#position-unavailable')。show();
休息;

case error.TIMEOUT:
$('。map-fullsize')。hide();
$('#weather-map')。hide();
$('#weather-data')。hide();
$('#timeout')。show();
休息;

case error.UNKNOWN_ERROR:
$('。map-fullsize')。hide();
$('#weather-map')。hide();
$('#weather-data')。hide();
$('#unknown-error')。show();
休息;




$ b $(document).ready(function(){
initialize();
});

我找不到这个代码有什么问题,有趣的是这个代码是在我获得权限被拒绝之前获取我的GPS位置。此问题是对我上一个问题的后续 >。如何解决我的问题?



在此先感谢您。

解决方案

您正在使用



navigator.geolocation.getCurrentPosition(showError,function(position){...})



规范为getCurrentPosition显示其他顺序的功能。原因是只有成功回调是强制性的;它必须先来。如果提供第二个参数,则是错误回调。第三个参数是一个选项对象。


I'm getting "Permission Denied" all the time when I'm trying to fetch my location with this code:

function initialize() {
    $('.map-fullsize').hide();
    $('#weather-map').hide();
    $('#weather-data').hide();

    if(geo_position_js.init()) {
        var waiting_time = $('#getting-position').html('Försöker att hitta din aktuella position. Var god vänta...');

        t = setTimeout(function() {
            waiting_time.html('Det tar längre tid att hitta din position, än vad det egentligen borde göra.<br><br><b>Tips</b><br>GPS-mottagaren har lättare att hitta dig om du är utomhus. Täta moln som till exempel vid ett åskoväder, kan göra det svårare för satelliterna att hämta din position.');
        }, 60000);

        geo_position_js.getCurrentPosition(show_position, function() {
            clearTimeout(t);
            $('#getting-position').html('<b>Ett fel uppstod</b><br>Din position kunde inte hittas. Se till att vara utomhus för bästa möjliga resultat och försök igen.');
        }, {
            enableHighAccuracy: true
        });
    } else {
        $('#getting-position').html('<b>Ett fel uppstod</b><br>Det verkar som att din webbläsare inte tillåter GPS-positionering.');
    }
}



function show_position(p) {
    $('.map-fullsize').show();
    $('#weather-map').show();
    $('#weather-data').show();
    $('#getting-position').hide();


    if(navigator.geolocation) {

        navigator.geolocation.getCurrentPosition(showError, function(position) {
            var latitude = position.coords.latitude;
            var longitude = position.coords.longitude;
            var accuracy = position.coords.accuracy;
            var speed = position.coords.speed;
            var altitude = position.coords.altitude;
            var heading = position.coords.heading;

            var coords = new google.maps.LatLng(latitude, longitude);
            var mapOptions = {
                              center: coords,
                              streetViewControl: false,

                              mapTypeControl: true,
                              navigationControlOptions: {
                                  style: google.maps.NavigationControlStyle.SMALL
                              },

                              zoomControl: true,
                              zoomControlOptions: {
                                  style: google.maps.ZoomControlStyle.SMALL,
                                  position: google.maps.ControlPosition.TOP_LEFT
                              },

                              mapTypeId: google.maps.MapTypeId.ROADMAP
                             };

            map = new google.maps.Map(
                document.getElementById('weather-map'), mapOptions
            );

            var marker = new google.maps.Marker({
                position: coords,
                map: map
            });

            var circle = new google.maps.Circle({
                center: coords,
                radius: accuracy,
                map: map,
                fillColor: '#3333ff',
                fillOpacity: 0.4,
                strokeColor: '#3333ff',
                strokeOpacity: 0.8,
                strokeWeight: 1
            });


            map.setCenter(coords);

            if(accuracy > 30) {
                map.fitBounds(circle.getBounds());
            } else {
                map.setZoom(14);
            }

            $('#weather-data').load('jquery-fetch/fetch-weatherdata.php?coor=' + latitude.toFixed(6).replace(/\./, '') + ',' + longitude.toFixed(6).replace(/\./, '') + '&coordinates=' + latitude.toFixed(6) + ',' + longitude.toFixed(6) + '&accuracy=' + accuracy + '&speed=' + speed + '&altitude=' + altitude + '&heading=' + heading);
        });

    } else {
        alert('Geolocation API stöds inte i din webbläsare');
    }



    function showError(error) {
        switch(error.code) {
            case error.PERMISSION_DENIED:
                $('.map-fullsize').hide();
                $('#weather-map').hide();
                $('#weather-data').hide();
                $('#permission-denied').show();
                break;

            case error.POSITION_UNAVAILABLE:
                $('.map-fullsize').hide();
                $('#weather-map').hide();
                $('#weather-data').hide();
                $('#position-unavailable').show();
                break;

            case error.TIMEOUT:
                $('.map-fullsize').hide();
                $('#weather-map').hide();
                $('#weather-data').hide();
                $('#timeout').show();
                break;

            case error.UNKNOWN_ERROR:
                $('.map-fullsize').hide();
                $('#weather-map').hide();
                $('#weather-data').hide();
                $('#unknown-error').show();
                break;
        }
    }
}


$(document).ready(function() {
    initialize();
});

I can't find anything wrong with this code and the funny thing with this is that the code is getting my GPS location before I'm getting "Permission Denied". This problem is an "follow-up" to my previous question. How can I fix my problem?

Thanks in advance.

解决方案

You are using

navigator.geolocation.getCurrentPosition(showError, function(position) {...})

The specification for getCurrentPosition shows the functions in the other order. The reason for this is that only the success callback is mandatory; it must come first. If a second argument is supplied, it's the error callback. A third argument is an options object.

这篇关于“许可被拒绝”当试图获取位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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