Problems with Safari and Google Maps API

1

I'm developing a page that uses geolocation and apparently everything works fine but in Safari version 11 I get an error geolocation My code is:

function initMap() {
var map;
var mapCenter;
var latit;
var lon;
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function (position) {
        latit = position.coords.latitude;
        lon = position.coords.longitude;
        mapCenter = new google.maps.LatLng(latit, lon);
        console.log(mapCenter)
        map = new google.maps.Map(document.getElementById('map'), {
            center: mapCenter,
            zoom: 15,
            mapTypeControl: false,
            fullscreenControl: true,
            fullscreenControlOptions: {
                position: google.maps.ControlPosition.RIGHT_BOTTOM
            },
            rotateControl: true,
            scaleControl: true
        });
        var markerP = new google.maps.Marker({
            position: mapCenter,
            map: map,
            title: 'Ubicacion actual',
            animation: google.maps.Animation.DROP,
        })
    }, function (error) {
        alert(error.code)
        switch (error) {
            case error.PERMISSION_DENIED:
                alert("No se ha permitido acceso a la posición del usuario")
                break;
            case error.POSITION_UNAVAILABLE:
                 alert("No se ha permitido acceder a la información de la posicón")
                 break;
            case error.TIMEOUT:
                alert("El servicio ha tardado demaciado tiempo en responder")
                break;
            default:
                  alert("Error desconocido")
        }

    })
} else {
    // Browser doesn't support Geolocation

}

}
 }

when you get to the% share% share sends me to the error function indicating "Unknown error" but this only happens in Safari.

In safari version 9 if it works, but I can not find in the documentation something that will guide me since if I run safari a few times and nothing works.

    
asked by Darian25 04.09.2018 в 21:54
source

1 answer

2

Try to use "https" in case you are not using it, also remember that navigator.geolocation.getCurrentPosition does not guarantee returning a result, since the user can deny the location permission making it impossible to locate it.

As a separate recommendation you can initialize the map with a default center not sent by the user.

    
answered by 04.09.2018 в 22:26