我有一个数组,我想将其值传递给路径以绘制多边形
var coords = new Array("25.1, 66", "25.5, 65", "21.3, 67");
无论坐标数字如何,我如何将上面数组中的模式“x, y”传递给 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
});
最佳答案
您可以使用 new google.maps.LatLng(coords[0].split(',')[0 而不是
.new google.maps.LatLng(coords[0])
],coords[0].split(',')[1])
但是,由于这会进行两次分割,我可能会将其提取到一个函数中 - 像这样:
function createLatLng(coordString) {
var a = coordString.split(',');
return new google.maps.LatLng(a[0],a[1]);
}
var triangleCoords = [
createLatLng(coords[0]),
....
];
为了获得额外的积分,您可以使用 for 循环:
var triangleCoords = [];
for (var i = 0; i < coords.length; i++) {
triangleCoords[i] = createLatLng(coords[i]);
}
关于javascript - 将值传递给 google.maps.LatLng,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8468037/