admin管理员组文章数量:1323524
I wonder if someone can point me to right direction here, I'm working with Google maps trying to shade assigned Post Code areas to a user, if I hardcode the latitude and longitude it works perfect like this;
var triangleCoordsLS12 = [
{lng: -1.558585, lat: 53.796545},
{lng: -1.558585, lat: 53.796545},
.....
];
but I'm trying to get the information from MySQL database vis PHP and JSON like this;
$.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);
}
}
}
})
I get an error Uncaught InvalidValueError: not an Array
on these lines:-
bermudaTriangleLS12 = new google.maps.Polygon({
I know error says not an Array
so how do I put the points in array? I'd be very grateful for your help.
I wonder if someone can point me to right direction here, I'm working with Google maps trying to shade assigned Post Code areas to a user, if I hardcode the latitude and longitude it works perfect like this;
var triangleCoordsLS12 = [
{lng: -1.558585, lat: 53.796545},
{lng: -1.558585, lat: 53.796545},
.....
];
but I'm trying to get the information from MySQL database vis PHP and JSON like this;
$.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);
}
}
}
})
I get an error Uncaught InvalidValueError: not an Array
on these lines:-
bermudaTriangleLS12 = new google.maps.Polygon({
I know error says not an Array
so how do I put the points in array? I'd be very grateful for your help.
1 Answer
Reset to default 4You need to construct the array first, and then use it when you create the polygon. In your code, you create a new polygon inside the "coordinates" loop, so you create a polygon with just one point on each loop.
//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);
Pseudocode my example:
For each coordinate {
add coordinate to array
}
construct-polygon(coordinate array)
Your code:
For each coordinate {
construct-polygon(coordinate)
}
本文标签: javascriptUncaught InvalidValueError not an ArrayStack Overflow
版权声明:本文标题:javascript - Uncaught InvalidValueError: not an Array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742131797a2422193.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论