2015-09-22 40 views
6

我不知道是否有人能指出我在這裏正確的方向,我用谷歌地圖想樹蔭分配郵編地區用戶的工作,如果我硬編碼緯度經度它的工作原理是這樣的;未捕獲InvalidValueError:不是數組

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讓我怎麼把點陣列?我非常感謝你的幫助。

回答

3

您需要先構造數組,然後在創建多邊形時使用它。在您的代碼中,您在「座標」循環內創建了一個新的多邊形,因此您可以在每個循環中僅創建一個多邊形。

//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) 
} 
+0

非常感謝你這工作,我校的孩子的錯誤。 – Tony

相關問題