Error if user does not release Geolocation

Asked

Viewed 383 times

0

I’m putting geolocation on a client site, only I’m having a problem doing some function that calls a alert() if the user does not give permission for the browser to use geolocation. What I want is just to display a alert() when the user denies. My current code

var map;
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();

function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    var latlng = new google.maps.LatLng(-18.8800397, -47.05878999999999);

    var options = {
        zoom: 5,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("mapa"), options);
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById("trajeto-texto"));

}


function getPosition(){

    if ( navigator.geolocation ){

        navigator.geolocation.getCurrentPosition( function( posicao ){

            var positions = new Array();

            positions['latitude'] = posicao.coords.latitude;
            positions['longitude'] = posicao.coords.longitude;

            initialize();

            var enderecoPartida = '08210-791, Brasil';
            var enderecoChegada = '08215-255, Brasil';

            var request = {
                origin: enderecoPartida,
                destination: enderecoChegada,
                travelMode: google.maps.TravelMode.DRIVING
            };

            directionsService.route(request, function(result, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    directionsDisplay.setDirections(result);
                }
            });


        } );

    }
}

getPosition();

I tried to leave that line Navigator.geolocation.getCurrentPosition( Function( position ){ thus Navigator.geolocation.getCurrentPosition( Function( position, error ){ but it didn’t work out.

  • Oops, I didn’t get to use the google maps api, but an idea here... You can set a function with settimeout(Function(){},time); and check if it has contents inside the div map, if it has content is pq loaded, correct?

  • yeah, and how would that check ? has some sample code there? I’m not so good with JS. I researched here and the problem seems to be in Ubuntu Firefox, which is what I use, but I saw people doing something with setTimeOut() only I was in English and ignored.

1 answer

1

watchPosition accept, as a second parameter, a callback if an error occurs - one of the possible ones being denied permission.

navigator.geolocation.watchPosition(function(position) {
  // Código caso o usuário tenha aceito.
},
function (error) { 
  if (error.code == error.PERMISSION_DENIED)
      // Caso o usuário tenha negado.
});

Reference.

  • The problem seems to be with Ubuntu’s Firefox (the one I’m using). I would have some tactic to circumvent this error ?

  • if you have any plugin in firefox try to disable all.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.