Google Maps API a.lat不是功能错误 [英] Google Maps API a.lat is not a function error

查看:124
本文介绍了Google Maps API a.lat不是功能错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was creating a code that can coordinate data from CSV file by using a split as the separator, and will calculate the distance between two input coordinates. But the result always shows the error a.lat is not a function. I already surf the web about this particular error type and can't seem to find the correct solution, can anyone please help me with this error.

这是我的协调示例:

Here is my coordinate example :

-6.168454914420734, 106.7574467
-6.16897225004169, 106.7570443

以下是我的代码:

And here is my code :

<!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>


推荐答案

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

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

程式码片段:

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>

这篇关于Google Maps API a.lat不是功能错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆