use*_*359 9 polygon google-maps-api-3
我找到了使用以下方法设置标记可见性的方法:
// 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);
Run Code Online (Sandbox Code Playgroud)
然后我使用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);
Run Code Online (Sandbox Code Playgroud)
现在我怎么能让这个形状不可见?
我的情况是我有一个复选框,用户检查是否看到多边形.第一次检查时,我将创建多边形,但随后的时间,我只想让多边形形状可见或不可见.
我正在转换一个虚拟地球应用程序,我可以在其中"显示"或"隐藏"包含多边形的图层,但我无法使用JavaScript找到针对Google API第3版的诀窍.
如果将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);
}
}
Run Code Online (Sandbox Code Playgroud)
现在你可以买BuyersEdge3.hide()和BuyersEdge3.show()
请享用!
| 归档时间: |
|
| 查看次数: |
6533 次 |
| 最近记录: |