Problems with Safari and Google Maps API


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);
        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) {
        switch (error) {
            case error.PERMISSION_DENIED:
                alert("No se ha permitido acceso a la posición del usuario")
            case error.POSITION_UNAVAILABLE:
                 alert("No se ha permitido acceder a la información de la posicón")
            case error.TIMEOUT:
                alert("El servicio ha tardado demaciado tiempo en responder")
                  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

1 answer


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