Google Maps API a.lat is not a function error
You are passing numbers (actually, strings) into the calcDistanc
function. It is using the google.maps.geometry.spherical.computeDistanceBetween
method which expects two google.maps.LatLng
objects (which have a .lat()
method). You need to calculate the distance between two google.maps.LatLng objects.
function calcDistance(p1, p2){ return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);}for(var e=0;e<4;e++){ var coor=byline[e].split(','); alert(calcDistance(coor[0],coor[1]));}
code snippet:
function calcDistance(p1, p2) { return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);}var allText = "-6.168454914420734, 106.7574467\n-6.16897225004169, 106.7570443";var byline = allText.split('\n');var path = [];for (var e = 0; e < byline.length; e++) { var coor = byline[e].split(','); path.push(new google.maps.LatLng(coor[0], coor[1])); if (path.length > 1) alert(calcDistance(path[0], path[1]));}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>