Problem with geometry.spherical.computeDistanceBetween () Google Maps

0

I found this code on the Internet and I tried to adapt it to my project, to get the current position.

In the jsp I have the references:

<script src="repartidores/js/jquery.js" type="text/javascript"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=MI_KEY&libraries=geometry"></script>
<script src="repartidores/js/posicionActual.js" type="text/javascript"></script>

And in current position.js:

var registrandoPosicion = false;
var idRegistroPosicion;
var ultimaPosicion;
var lat;
var lng;

function registrarPosicion() {
    if (registrandoPosicion) {
        registrandoPosicion = false;
        navigator.geolocation.clearWatch(idRegistroPosicion);
        limpiarUbicacion();
    } else {
        idRegistroPosicion = navigator.geolocation.watchPosition(exitoRegistroPosicion, falloRegistroPosicion, {
            enableHighAccuracy: true,
            maximumAge: 30000,
            timeout: 27000
        });
    }
}

function exitoRegistroPosicion(position) {
    if (!registrandoPosicion) {
        // Es la primera vez 
        registrandoPosicion = true;
        lat = position.coords.latitude;
        lng = position.coords.longitude;
        ultimaPosicion = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

        $.ajax({
            type: 'GET',
            url: 'localhost:8084/appRepartidores/PosicionActualServlet',
            data:{"latitud": lat, "longitud": lng}
        });

    } else {
        var posicionActual = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        if (google.maps.geometry.shperical.computeDistanceBetween(posicionActual, ultimaPosicion) > 100){
            ultimaPosicion = posicionActual;
            lat = position.coords.latitude;
            lng = position.coords.longitude;

            $.ajax({
                type: 'GET',
                url: 'localhost:8084/appRepartidores/PosicionActualServlet',
                data:{"latitud": lat, "longitud": lng}
            });
        }
    }

}

function falloRegistroPosicion() {
    alert('No se pudo determinar la ubicación');
    limpiarUbicacion();
}

function limpiarUbicacion() {
    ultimaPosicionUsuario = new google.maps.LatLng(0, 0);
}

$('#localizar').on('click', function(e) {
    e.preventDefault();
    registrarPosicion();
});

When I run it and press the button, it throws me this error:

SCRIPT5007: Unable to get property 'computeDistanceBetween' of undefined or null reference positionActual.js (37,13)

    
asked by Juan Manuel 01.12.2016 в 15:52
source

1 answer

1

You are using

google.maps.geometry.spherical.computeDistanceBetween

And he tells you

Unable to get property 'computeDistanceBetween' of undefined

Which means that

google.maps.geometry.shperical

It's undefined.

The reason why this should be happening is probably that you are not including the geometry library from google maps.

If you now include the library using the URL

https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY

You must include it instead as

https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry

Edit: As indicated by @juanmanuel, it was badly written "spherical" and I took it from his question as it is, without noticing. Corrected and grateful.

    
answered by 03.03.2017 / 12:39
source