Use google maps computeDistanceBetween to get the closest location returns NaN

前端 未结 1 933
星月不相逢
星月不相逢 2020-12-15 11:12

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

相关标签:
1条回答
  • 2020-12-15 11:42

    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));
    });
    
    0 讨论(0)
提交回复
热议问题