Skip to content Skip to sidebar Skip to footer

Use Google Maps Computedistancebetween To Get The Closest Location Returns Nan

Ok so I have set up a map and an autocomplete field on a testing site. My goal is to get the user to input his/her address into the field and then when he/she clicks confirm work o

Solution 1:

google.maps.geometry.spherical.computeDistanceBetween requires that you feed it two google.maps.LatLng objects whereas you are using strings, and that won't work. The following should...

$confirm.click(function(){
    var _street = $('#address').val();
    var place = autocomplete.getPlace();
    var _coordinates = place.geometry.location; //a google.maps.LatLng object
    var _kCord = new google.maps.LatLng(-36.874694, 174.735292);
    var _pCord = new google.maps.LatLng(-36.858317, 174.782284);

    console.log(_coordinates);

    console.log(google.maps.geometry.spherical.computeDistanceBetween(_pCord, _coordinates));
    console.log(google.maps.geometry.spherical.computeDistanceBetween(_kCord, _coordinates));
});

Post a Comment for "Use Google Maps Computedistancebetween To Get The Closest Location Returns Nan"