首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Google的版本3 API设置多边形的可见性?

如何使用Google的版本3 API设置多边形的可见性?
EN

Stack Overflow用户
提问于 2011-12-01 06:54:45
回答 5查看 7.5K关注 0票数 9

我已经找到了使用以下内容设置标记可见性的方法:

代码语言:javascript
运行
复制
            // 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)使其可见/不可见。

但是如何对多边形执行相同的操作呢?

下面是我如何设置我的多边形:

代码语言:javascript
运行
复制
        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做这件事。

EN

回答 5

Stack Overflow用户

发布于 2011-12-01 07:31:36

如果将strokeOpacity和fillOpacity设置为零并将多边形重置到地图,则可以执行此操作。

这是Polygon原型的一个小技巧(这意味着您将可以在所有Polygon对象中访问它),它将为您完成这件事

代码语言:javascript
运行
复制
// 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()

享受吧!

票数 8
EN

Stack Overflow用户

发布于 2015-03-24 02:26:29

您可以使用:

代码语言:javascript
运行
复制
BuyersEdge3.setOptions({visible:false});
票数 3
EN

Stack Overflow用户

发布于 2015-05-27 15:01:21

代码语言:javascript
运行
复制
    if (BuyersEdge3.map)
    {
        BuyersEdge3.setMap(null);
    } else {
        BuyersEdge3.setMap(map);
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8334197

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档