我想在加载时在我的谷歌地图中显示一些多边形,并分别添加每个多边形。但它是一个漫长的过程,我想要的只是创建一个循环并使初始化过程和其他事情变得更容易。我在这里插入代码
var destinationsi = ["destinations1", "destinations2", "destinations3", "destinations4"];
var polygon = ["polygon1", "polygon2", "polygon3", "polygon4"];
var polygonOptions = ["polygonOptions1", "polygonOptions2", "polygonOptions3", "polygonOptions4"];
for (polygoni = 1; polygoni <= 4; polygoni++ )
{
var polygoni = new google.maps.Polygon(polygonOptionsi);
var polygonOptionsi = {path: destinationsi, strokeColor:"#EDE3D0", strokeWeight:"2", fillColor: '#598BE2', fillOpacity: 0.35};
polygoni.setMap(map);
}
但是地图中没有显示任何多边形。 你怎么让这个开心呢?
答案 0 :(得分:0)
你的JavaScript有各种各样的错误。
试试这个:
//Replace ... with actual latitude and longitude points/arrays
var destinations = [[{lat: -34, lng: 151}, ...], ...];
var polygonOptions;
var polygon;
for (var i = 0; i < destinations.length; i++) {
polygonOptions = {
paths: destinations[i], //'path' was not a valid option - using 'paths' array according to Google Maps API
strokeColor: "#EDE3D0",
strokeWeight: "2",
fillColor: '#598BE2',
fillOpacity: 0.35
};
polygon = new google.maps.Polygon(polygonOptions);
//map needs to be defined somewhere outside of this loop
//I'll assume you already have that.
polygon.setMap(map);
}