我已经找到了使用以下内容设置标记可见性的方法:
// create the marker
blueMarker = new google.maps.Marker({
position: new google.maps.LatLng(33.514428, -112.29056534285377),
draggable: true,
raiseOnDrag: false,
icon: './Images/blue3Marker.png',
shapeType: 'BuyersEdgeArea',
shapeID: '3'
});
// set the marker on the map
blueMarker.setMap(map);然后我使用blueMarker.setVisible(false)或blueMarker.setVisible(true)使其可见/不可见。
但是如何对多边形执行相同的操作呢?
下面是我如何设置我的多边形:
BuyersEdge3 = new google.maps.Polygon({
clickable: true,
paths: BuyersEdgePath3,
strokeColor: '#000000',
strokeOpacity: 1,
strokeWeight: 2,
fillColor: ' #810541 ',
fillOpacity: 0.35
});
// set the shape on the map
BuyersEdge3.setMap(map);现在我该如何使这个形状不可见呢?
我的情况是,我有一个复选框,用户可以在其中检查是否看到多边形。第一次检查时,我将创建多边形,但随后,我只想使多边形形状可见或不可见。
我正在转换一个虚拟地球应用程序,在那里我可以只“显示”或“隐藏”一个带有多边形的图层,但我找不到一些东西来使用JavaScript为Google API版本3做这件事。
发布于 2011-12-01 07:31:36
如果将strokeOpacity和fillOpacity设置为零并将多边形重置到地图,则可以执行此操作。
这是Polygon原型的一个小技巧(这意味着您将可以在所有Polygon对象中访问它),它将为您完成这件事
// this is a visibility flag. don't change it manually
google.maps.Polygon.prototype._visible = true;
// this will save opacity values and set them to 0, and rebound the polygon to the map
google.maps.Polygon.prototype.hide = function(){
if (this._visible) {
this._visible = false;
this._strokeOpacity = this.strokeOpacity;
this._fillOpacity = this.fillOpacity;
this.strokeOpacity = 0;
this.fillOpacity = 0;
this.setMap(this.map);
}
}
// this will restore opacity values. and rebound the polygon to the map
google.maps.Polygon.prototype.show = function() {
if (!this._visible) {
this._visible = true;
this.strokeOpacity = this._strokeOpacity;
this.fillOpacity = this._fillOpacity;
this.setMap(this.map);
}
}现在你可以做BuyersEdge3.hide()和BuyersEdge3.show()了
享受吧!
发布于 2015-03-24 02:26:29
您可以使用:
BuyersEdge3.setOptions({visible:false});发布于 2015-05-27 15:01:21
if (BuyersEdge3.map)
{
BuyersEdge3.setMap(null);
} else {
BuyersEdge3.setMap(map);
}https://stackoverflow.com/questions/8334197
复制相似问题