Google Maps API a.lat不是函数错误

时间:2016-04-15 07:09:31

标签: javascript arrays google-maps latitude-longitude

我正在创建一个代码,可以使用分割作为分隔符来协调CSV文件中的数据,并计算两个输入坐标之间的距离。但结果总是显示错误a.lat不是函数。我已经在网上浏览了这个特定的错误类型,似乎无法找到正确的解决方案,任何人都可以帮我解决这个错误。

这是我的坐标示例:

-6.168454914420734, 106.7574467
-6.16897225004169, 106.7570443

这是我的代码:

<!DOCTYPE html>
<html>
<head>
<script src="http://maps.googleapis.com/maps/api/js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry"></script>

<script>
function calcDistance(p1, p2){
  return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
}

function encodeLatLngPolygon(array) {

var polyOptions = {
strokeColor: '#000000',
strokeOpacity: 1.0,
strokeWeight: 3
  }
  poly = new google.maps.Polyline(polyOptions);

var path = poly.getPath();

for(var i=0;i<array.length;i++) {
    var xyz = new google.maps.LatLng(parseFloat(array[i][0]).toFixed(2), parseFloat(array[i][1]).toFixed(2));
    path.push(xyz);            

}

var code = google.maps.geometry.encoding.encodePath(path)

return code;
}

function readTextFile(file)
{
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, true);
    rawFile.onreadystatechange = function ()
    {
        if(rawFile.readyState === 4)
        {
            if(rawFile.status === 200 || rawFile.status == 0)
            {
                var allText = rawFile.responseText;

                var byline = allText.split('\n');

                for(var e=0;e<4;e++){
                var coor=byline[e].split(',');
                alert(calcDistance(coor[0],coor[1]));

                }



            }
        }
    }
    rawFile.send(null);
}

function initialize() {
  var mapProp = {
    center:new google.maps.LatLng(51.508742,-0.120850),
    zoom:5,
    mapTypeId:google.maps.MapTypeId.ROADMAP
  };
  var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);

  readTextFile("DaerahG.csv");


}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<title>kenny</title>
</head>

<body>
<div id="googleMap" style="width:1000px;height:700px; float:left;"></div>

</body>

</html>

1 个答案:

答案 0 :(得分:13)

您正在将数字(实际上是字符串)传递到calcDistanc函数中。它使用google.maps.geometry.spherical.computeDistanceBetween方法,该方法需要两个google.maps.LatLng个对象(具有.lat()方法)。您需要计算两个google.maps.LatLng对象之间的距离。

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]));
}

代码段

&#13;
&#13;
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]));
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
&#13;
&#13;
&#13;