I have an array and I want to pass its values to a path to draw a polygon
var coords = new Array("25.1, 66", "25.5, 65", "21.3, 67");
regardless the coordinates numbers, how can i pass this pattern “x, y” as in the array above to google.maps.LatLng
var triangleCoords = [
new google.maps.LatLng(coords[0]),
new google.maps.LatLng(coords[1]),
new google.maps.LatLng(coords[2]),
];
// Construct the polygon
// Note that we don't specify an array or arrays, but instead just
// a simple array of LatLngs in the paths property
draw = new google.maps.Polygon({
paths: triangleCoords,
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#FF0000",
fillOpacity: 0.35
});
instead of
new google.maps.LatLng(coords[0])you can donew google.maps.LatLng(coords[0].split(',')[0],coords[0].split(',')[1]).However, since that’s doing the split twice, I’d probably pull it out into a function – something like this:
For extra credit, you can then use a for loop: