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.
You 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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With