Error if user does not release GeoLocation

0

I'm putting geolocation on a client's website, but I'm having a problem doing some function that calls a alert() if the user does not give the browser permission to use the geolocation. What I want is to only display alert() when the user deny. 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 this line navigator.geolocation.getCurrentPosition (function (position) { in this way navigator.geolocation.getCurrentPosition (function (position, error) {) but did not right.

    
asked by anonymous 10.05.2015 / 06:35

1 answer

1

watchPosition accepts, as the second parameter, a callback in the event of an error - one of the possible permissions being denied.

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.

    
10.05.2015 / 09:53