在google maps javascript中按函数构建多个多边形
我想在谷歌地图中建立很多区域,并用多边形定义每个区域。 如果我一个接一个地执行此操作,则不会出现问题(在初始化函数中): 现在我想有一个函数,只需要输入坐标来构建多边形,就像这样。但仅调用该函数即可停止渲染其他多边形,因此我知道调用该函数时存在问题:在google maps javascript中按函数构建多个多边形,javascript,google-maps-api-3,Javascript,Google Maps Api 3,我想在谷歌地图中建立很多区域,并用多边形定义每个区域。 如果我一个接一个地执行此操作,则不会出现问题(在初始化函数中): 现在我想有一个函数,只需要输入坐标来构建多边形,就像这样。但仅调用该函数即可停止渲染其他多边形,因此我知道调用该函数时存在问题: iName = new drawPolygon(polyName, coords); iName.setMap(map); 函数如下所示: function drawPolygon(polyName, coords) { polyN
iName = new drawPolygon(polyName, coords);
iName.setMap(map);
函数如下所示:
function drawPolygon(polyName, coords) {
polyName = new google.maps.Polygon({
paths: coords,
strokeColor: 'darkgreen',
strokeOpacity: 0.3,
strokeWeight: 1,
//fillColor: 'green',
fillOpacity: 0.05
});
//highlights polygon when mouseover
google.maps.event.addListener(polyName, 'mouseover', function () {
polyName.setOptions({ fillColor: 'yellow', fillOpacity: 0.25 });
});
}
关于为什么,我怎么称呼它是错的,有什么帮助吗?drawPolygon没有返回语句。它返回
null
null
没有.setMap方法。在geocodezip的答案上展开,只需在函数中添加一个return语句
function drawPolygon(polyName, coords) {
polyName = new google.maps.Polygon({
paths: coords,
strokeColor: 'darkgreen',
strokeOpacity: 0.3,
strokeWeight: 1,
fillOpacity: 0.05
});
//highlights polygon when mouseover
google.maps.event.addListener(polyName, 'mouseover', function () {
polyName.setOptions({ fillColor: 'yellow', fillOpacity: 0.25 });
});
return polyName;
}
在这种情况下,我也倾向于不用麻烦将polyName
作为参数传递到函数中。在调用drawPolygon之前,您不必向我们展示创建polyName变量的代码。但我认为你没有做任何需要你做的特别聪明的事情
如此重构:
iName = new drawPolygon(coords);
iName.setMap(map);
function drawPolygon(coords) {
var polyName = new google.maps.Polygon({
paths: coords,
strokeColor: 'darkgreen',
strokeOpacity: 0.3,
strokeWeight: 1,
fillOpacity: 0.05
});
//highlights polygon when mouseover
google.maps.event.addListener(polyName, 'mouseover', function () {
polyName.setOptions({ fillColor: 'yellow', fillOpacity: 0.25 });
});
return polyName;
}
谢谢,现在已经快到了……几个小家伙。我确实忘记返回某些内容:0
iName = new drawPolygon(coords);
iName.setMap(map);
function drawPolygon(coords) {
var polyName = new google.maps.Polygon({
paths: coords,
strokeColor: 'darkgreen',
strokeOpacity: 0.3,
strokeWeight: 1,
fillOpacity: 0.05
});
//highlights polygon when mouseover
google.maps.event.addListener(polyName, 'mouseover', function () {
polyName.setOptions({ fillColor: 'yellow', fillOpacity: 0.25 });
});
return polyName;
}