【问题标题】:Adding google.maps.latLng objects to list causes [Uncaught TypeError: google.maps.latLng is not a function]将 google.maps.latLng 对象添加到列表会导致 [Uncaught TypeError: google.maps.latLng is not a function]
【发布时间】:2015-10-30 12:12:18
【问题描述】:

我正在对服务器进行 ajax 调用以获取纬度和经度数据并将它们推送到包含 google.maps.latLng 坐标的列表。我正在做一些计算,因为包含数据的矩阵来自一个稀疏矩阵(当然会被重写,因为到目前为止它很丑)。我知道我的 tempLongtempLat 值是正确的,但是当我执行 ajax 调用时,我不断收到 [Uncaught TypeError: google.maps.latLng is not a function]。我怀疑这与 ajax 执行的异步调用有关,但我不确定。我查看了this 的帖子并决定将我所有的代码放在.done{} 函数中,但这并没有帮助。

无论如何,这是我的代码:

function getMultipleArrays(){
var div = document.getElementById("map2");

$.get('getJSONObject', function(data) {

}).done(function(data){
    var minLat = data.minLat;
    var minLong = data.minLong;
    var m = data.m;
    var n = data.n;
    var val = data.val;
    var col = data.col;
    var row = data.row;
    var list =[];

    var minLatCoo= minLat*1000000;
    var minLongCoo= minLong*1000000;
    var i, j, k,zeroIndex;

    for(i=0; i<m;i++){
        zeroIndex=0;
        for(k=row[i]; k<row[i+1];k++){
            j= col[k];
            while(zeroIndex<j){
                zeroIndex++;
            }
            minLatCoo= minLat*1000000;
            minLongCoo= minLong*1000000;
            for(var l=val[k]; l>0;l--){
                minLatCoo+=zeroIndex;
                var tempLat = minLatCoo/1000000;
                minLongCoo+=i;
                var tempLong = minLongCoo/1000000;
                list.push(new google.maps.latLng(tempLong,tempLat));
                console.log(tempLong + " - " + tempLat);
            }
            zeroIndex++;
        }
    }
    console.log(list.length);
    console.log("DONE!");
    return list;
});}

错误发生在list.push(new google.maps.latLng(tempLong,tempLat)); 行。任何想法为什么它会失败?

【问题讨论】:

    标签: javascript ajax google-maps-api-3


    【解决方案1】:

    类是 LatLng,而不是 latLng。 Javascript 区分大小写;应该是

    list.push(new google.maps.LatLng(tempLong,tempLat));
    

    ...不是

    list.push(new google.maps.latLng(tempLat, tempLong));
    

    你也做了new google.maps.latLng(tempLong,tempLat),据我推断你的纬度和经度混淆了。

    见: https://developers.google.com/maps/documentation/javascript/reference#LatLng

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-15
      • 2021-12-16
      • 2018-04-20
      相关资源
      最近更新 更多