【问题标题】:Uncaught InvalidValueError: not an Array未捕获的 InvalidValueError:不是数组
【发布时间】:2015-12-19 06:27:10
【问题描述】:

我想知道是否有人可以在这里为我指出正确的方向,如果我硬编码 latitudelongitude,我正在使用 Google 地图尝试为用户隐藏分配的邮政编码区域 像这样完美运行;

var triangleCoordsLS12 = [
        {lng: -1.558585,  lat: 53.796545}, 
        {lng: -1.558585,  lat: 53.796545},
        .....
];

但我正试图从 MySQL 数据库中获取信息,例如 PHP 和 JSON;

$.ajax({
          type:'POST',
          url:'test.php',
          success:function(data){
             var resultArray = JSON.parse(data);
               for (var i=0; i<resultArray.length; i++) {
                  var triangleCoordsLS12 = new google.maps.LatLng(resultArray[i].lat, resultArray[i].lng);

          if(location.uname == 'John Smith'){
            bermudaTriangleLS12 = new google.maps.Polygon({
                paths: triangleCoordsLS12,
                strokeColor: '#ff0000',
                strokeOpacity: 0.8,
                strokeWeight: 1,
                fillColor: '#ff0000',
                fillOpacity: 0.30
            });
           bermudaTriangleLS12.setMap(map);
        } else if(location.uname == 'Bruce Brassington'){
             bermudaTriangleLS12 = new google.maps.Polygon({
               paths: triangleCoordsLS12,
               strokeColor: '#FFcc00',
               strokeOpacity: 0.8,
               strokeWeight: 1,
               fillColor: '#FFcc00',
               fillOpacity: 0.25
             });
            bermudaTriangleLS12.setMap(map);                 
        }
      } 
    }
 })  

我在以下几行收到错误Uncaught InvalidValueError: not an Array:-

bermudaTriangleLS12 = new google.maps.Polygon({

我知道错误说不是Array 那么如何将点放入数组中?非常感谢您的帮助。

【问题讨论】:

    标签: javascript php jquery mysql google-maps


    【解决方案1】:

    您需要先构造数组,然后在创建多边形时使用它。在您的代码中,您在“坐标”循环内创建了一个新多边形,因此您创建了一个多边形,每个循环上只有一个点。

    //build the array
    var resultArray = JSON.parse(data);
    var triangleCoordsLS12 = []
    for (var i=0; i<resultArray.length; i++) {
        triangleCoordsLS12[i] = new google.maps.LatLng(resultArray[i].lat, resultArray[i].lng);
    }
    //use the array as coordinates
    bermudaTriangleLS12 = new google.maps.Polygon({
             paths: triangleCoordsLS12,
             trokeColor: '#ff0000',
             strokeOpacity: 0.8,
             strokeWeight: 1,
             fillColor: '#ff0000',
             fillOpacity: 0.30
    });
    bermudaTriangleLS12.setMap(map);
    

    伪代码我的例子:

    For each coordinate {
        add coordinate to array
    }
    construct-polygon(coordinate array)
    

    您的代码:

    For each coordinate {
        construct-polygon(coordinate)
    }
    

    【讨论】:

    • 当你将 string 传递给 Polygon 数据数组时也会发生错误,所以请确保你已经使用了 浮动类型
    猜你喜欢
    • 2014-05-01
    • 2016-11-28
    • 2016-11-17
    • 2017-07-20
    • 2019-09-26
    • 2015-12-29
    • 2021-09-09
    • 2017-07-31
    • 2016-11-02
    相关资源
    最近更新 更多