Google Maps API a.lat is not a function error

流过昼夜 提交于 2019-12-01 09:24:08

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!