qq.maps.Polygon 类

继承自MVCObject

多边形覆盖物。

构造函数

构造函数
Polygon(options:PolygonOptions)

方法

方法 返回值 说明
getBounds() LatLngBounds 返回多边形的地理区域范围。
getMap() Map 返回多边形所在的map对象
getPath() MVCArray.<LatLng> 获取多边形的经纬度坐标数组。
getVisible() Boolean 获取多边形覆盖物的可见性。
getZIndex() Number 获取多边形覆盖物的zIndex值。
setMap(map:Map) none 设置多边形所在的map对象
setPath(path:Array.<LatLng> | Array.<Array.<LatLng>>) none 设置多边形轮廓的经纬度坐标数组,若为二维数组则表现为环多边形。
setVisible(visible:Boolean) none 设置多边形的可见性。
setZIndex(zIndex:Number) none 设置多变心覆盖物的zIndex。
setOptions(options:PolygonOptions) none 设置多边形参数。

事件

事件名 参数 说明
map_changed none 当此多边形所在地图更改时会触发此事件。
visible_changed none 当此多边形可见性更改时会触发此事件。
zindex_changed none 当此多边形zIndex更改时会触发此事件。
click event:MouseEvent 点击此多边形后会触发此事件。
dblclick event:MouseEvent 双击多边形后会触发此事件。
rightclick event:MouseEvent 右键点击多边形后会触发此事件。
mousedown event:MouseEvent 鼠标在多边形内按下触发此事件。
mouseup event:MouseEvent 鼠标在多边形上抬起时触发此事件。
mouseover event:MouseEvent 当鼠标进入多边形区域时会触发此事件。
mouseout event:MouseEvent 鼠标离开多边形时触发此事件。
mousemove event:MouseEvent 鼠标在多边形内移动时触发此事件。
insertNode event:EditorEvent 编辑多边形添加节点时触发此事件。
removeNode event:EditorEvent 编辑多边形删除节点时触发此事件。
adjustNode event:EditorEvent 编辑多边形移动节点时触发此事件。

实例

例子 1:

本示例中,介绍如何修改边框、填充色及透明度

JavaScript
var map = new qq.maps.Map(document.getElementById("container"), {
    // 地图的中心地理坐标。
    center: new qq.maps.LatLng(39.916527, 116.397128),
    zoom: 14
});
var path = [
    new qq.maps.LatLng(39.926516, 116.389366),
    new qq.maps.LatLng(39.924870, 116.403270),
    new qq.maps.LatLng(39.918090, 116.406274),
    new qq.maps.LatLng(39.909466, 116.397863),
    new qq.maps.LatLng(39.913021, 116.387134)
];
var polygon = new qq.maps.Polygon({
    map: map,
    path: path,
    strokeColor: new qq.maps.Color(0, 0, 0, 0.8),
    fillColor: qq.maps.Color.fromHex("#FFFFFF", 0.5)
});
JavaScript+HTML
<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>腾讯地图-简单地图示例</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77"></script>
    <style type="text/css">
        html,
        body {
            height: 100%;
            margin: 0px;
            padding: 0px
        }
        #container {
            width: 100%;
            height: 100%
        }
    </style>
</head>

<body>
    <div id="container"></div>
    <script type="text/javascript">
        var map = new qq.maps.Map(document.getElementById("container"), {
            // 地图的中心地理坐标。
            center: new qq.maps.LatLng(39.916527, 116.397128),
            zoom: 14
        });
        var path = [
            new qq.maps.LatLng(39.926516, 116.389366),
            new qq.maps.LatLng(39.924870, 116.403270),
            new qq.maps.LatLng(39.918090, 116.406274),
            new qq.maps.LatLng(39.909466, 116.397863),
            new qq.maps.LatLng(39.913021, 116.387134)
        ];
        var polygon = new qq.maps.Polygon({
            map: map,
            path: path,
            strokeColor: new qq.maps.Color(0, 0, 0, 0.8),
            fillColor: qq.maps.Color.fromHex("#FFFFFF", 0.5)
        });
    </script>
</body>

</html>

例子 2:

本示例中,介绍如何开启编辑功能

JavaScript
var map = new qq.maps.Map(document.getElementById("container"), {
    // 地图的中心地理坐标。
    center: new qq.maps.LatLng(39.916527, 116.397128),
    zoom: 14
});
var path = [
    new qq.maps.LatLng(39.926516, 116.389366),
    new qq.maps.LatLng(39.924870, 116.403270),
    new qq.maps.LatLng(39.918090, 116.406274),
    new qq.maps.LatLng(39.909466, 116.397863),
    new qq.maps.LatLng(39.913021, 116.387134)
];
var polygon = new qq.maps.Polygon({
    map: map,
    path: path,
    editable: true
});
JavaScript+HTML
<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>腾讯地图-简单地图示例</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77"></script>
    <style type="text/css">
        html,
        body {
            height: 100%;
            margin: 0px;
            padding: 0px
        }
        #container {
            width: 100%;
            height: 100%
        }
    </style>
</head>

<body>
    <div id="container"></div>
    <script type="text/javascript">
        var map = new qq.maps.Map(document.getElementById("container"), {
            // 地图的中心地理坐标。
            center: new qq.maps.LatLng(39.916527, 116.397128),
            zoom: 14
        });
        var path = [
            new qq.maps.LatLng(39.926516, 116.389366),
            new qq.maps.LatLng(39.924870, 116.403270),
            new qq.maps.LatLng(39.918090, 116.406274),
            new qq.maps.LatLng(39.909466, 116.397863),
            new qq.maps.LatLng(39.913021, 116.387134)
        ];
        var polygon = new qq.maps.Polygon({
            map: map,
            path: path,
            editable: true
        });
    </script>
</body>

</html>

例子 3:

本示例中,介绍多边形覆盖物的一些接口使用

JavaScript
var init = function() {
    var center = new qq.maps.LatLng(39.916527, 116.397128);
    var map = new qq.maps.Map(document.getElementById('container'), {
        center: center,
        zoom: 13
    });
    var path1 = [
        new qq.maps.LatLng(39.930131, 116.395500),
        new qq.maps.LatLng(39.911437, 116.377476),
        new qq.maps.LatLng(39.911305, 116.417301)
    ];
    var path2 = [
        new qq.maps.LatLng(39.926516, 116.389366),
        new qq.maps.LatLng(39.924870, 116.403270),
        new qq.maps.LatLng(39.918090, 116.406274),
        new qq.maps.LatLng(39.909466, 116.397863),
        new qq.maps.LatLng(39.913021, 116.387134)
    ];
    var polygon = new qq.maps.Polygon({
        path: path1,
        strokeColor: '#000000',
        strokeWeight: 5,
        fillColor: '#111111',
        map: map
    });
    //setMap()设置多边形覆盖物所在地图
    var mapM = document.getElementById("mapM");
    qq.maps.event.addDomListener(mapM, "click", function() {
        polygon.setVisible(true);
        if (polygon.getMap()) {
            polygon.setMap(null);
        } else {
            polygon.setMap(map);
        }
    });
    //setCursor()设置鼠标经过多边形覆盖物的样式
    var curF = true;
    var cursor = document.getElementById("cursor");
    qq.maps.event.addDomListener(cursor, "click", function() {
        polygon.setMap(map);
        polygon.setVisible(true);
        if (curF) {
            curF = false;
            polygon.setCursor("default");
        } else {
            curF = true;
            polygon.setCursor("pointer");
        }
    });
    //setFillColor()设置多边形覆盖物的填充颜色
    var fillCF = true;
    var fillC = document.getElementById("fillC");
    qq.maps.event.addDomListener(fillC, "click", function() {
        polygon.setMap(map);
        polygon.setVisible(true);
        if (fillCF) {
            fillCF = false;
            polygon.setFillColor("#0f0");
        } else {
            fillCF = true;
            polygon.setFillColor("#111");
        }
    });
    //setPath()设置多边形覆盖物的顶点坐标
    var pathF = true;
    var path = document.getElementById("path");
    qq.maps.event.addDomListener(path, "click", function() {
        polygon.setMap(map);
        polygon.setVisible(true);
        if (pathF) {
            pathF = false;
            polygon.setPath(path2);
        } else {
            pathF = true;
            polygon.setPath(path1);
        }
    });
    //setStrokeColor()设置多边形覆盖物的边线颜色
    var strokeCF = true;
    var strokeC = document.getElementById("strokeC");
    qq.maps.event.addDomListener(strokeC, "click", function() {
        polygon.setMap(map);
        polygon.setVisible(true);
        if (strokeCF) {
            strokeCF = false;
            polygon.setStrokeColor("#1c29d8");
        } else {
            strokeCF = true;
            polygon.setStrokeColor("#000");
        }
    });
    //setStrokeDashStyle()设置多边形覆盖物的边线样式
    var dashF = true;
    var dash = document.getElementById("dash");
    qq.maps.event.addDomListener(dash, "click", function() {
        polygon.setMap(map);
        polygon.setVisible(true);
        if (dashF) {
            dashF = false;
            polygon.setStrokeDashStyle("dash");
        } else {
            dashF = true;
            polygon.setStrokeDashStyle("solid");
        }
    });
    //setStrokeWeight()设置多边形覆盖物的边线宽度
    var strokeWeightF = true;
    var strokeWeight = document.getElementById("strokeWeight");
    qq.maps.event.addDomListener(strokeWeight, "click", function() {
        polygon.setMap(map);
        polygon.setVisible(true);
        if (strokeWeightF) {
            strokeWeightF = false;
            polygon.setStrokeWeight(10);
        } else {
            strokeWeightF = true;
            polygon.setStrokeWeight(5);
        }
    });
    //setVisible()设置多边形覆盖物的可见性
    var visibleTF = true;
    var visibleT = document.getElementById("visibleT");
    qq.maps.event.addDomListener(visibleT, "click", function() {
        polygon.setMap(map);
        if (visibleTF) {
            visibleTF = false;
            polygon.setVisible(false);
        } else {
            visibleTF = true;
            polygon.setVisible(true);
        }
    });
}
JavaScript+HTML
<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>添加多边形区域覆盖物</title>
    <style type="text/css">
        * {
            margin: 0px;
            padding: 0px;
        }
        body,
        button,
        input,
        select,
        textarea {
            font: 12px/16px Verdana, Helvetica, Arial, sans-serif;
        }
        #info {
            width: 603px;
            padding-top: 3px;
            overflow: hidden;
        }
        .btn {
            width: 190px;
        }
    </style>
    <script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77"></script>
    <script>
        var init = function() {
            var center = new qq.maps.LatLng(39.916527, 116.397128);
            var map = new qq.maps.Map(document.getElementById('container'), {
                center: center,
                zoom: 13
            });
            var path1 = [
                new qq.maps.LatLng(39.930131, 116.395500),
                new qq.maps.LatLng(39.911437, 116.377476),
                new qq.maps.LatLng(39.911305, 116.417301)
            ];
            var path2 = [
                new qq.maps.LatLng(39.926516, 116.389366),
                new qq.maps.LatLng(39.924870, 116.403270),
                new qq.maps.LatLng(39.918090, 116.406274),
                new qq.maps.LatLng(39.909466, 116.397863),
                new qq.maps.LatLng(39.913021, 116.387134)
            ];
            var polygon = new qq.maps.Polygon({
                path: path1,
                strokeColor: '#000000',
                strokeWeight: 5,
                fillColor: '#111111',
                map: map
            });
            //setMap()设置多边形覆盖物所在地图
            var mapM = document.getElementById("mapM");
            qq.maps.event.addDomListener(mapM, "click", function() {
                polygon.setVisible(true);
                if (polygon.getMap()) {
                    polygon.setMap(null);
                } else {
                    polygon.setMap(map);
                }
            });
            //setCursor()设置鼠标经过多边形覆盖物的样式
            var curF = true;
            var cursor = document.getElementById("cursor");
            qq.maps.event.addDomListener(cursor, "click", function() {
                polygon.setMap(map);
                polygon.setVisible(true);
                if (curF) {
                    curF = false;
                    polygon.setCursor("default");
                } else {
                    curF = true;
                    polygon.setCursor("pointer");
                }
            });
            //setFillColor()设置多边形覆盖物的填充颜色
            var fillCF = true;
            var fillC = document.getElementById("fillC");
            qq.maps.event.addDomListener(fillC, "click", function() {
                polygon.setMap(map);
                polygon.setVisible(true);
                if (fillCF) {
                    fillCF = false;
                    polygon.setFillColor("#0f0");
                } else {
                    fillCF = true;
                    polygon.setFillColor("#111");
                }
            });
            //setPath()设置多边形覆盖物的顶点坐标
            var pathF = true;
            var path = document.getElementById("path");
            qq.maps.event.addDomListener(path, "click", function() {
                polygon.setMap(map);
                polygon.setVisible(true);
                if (pathF) {
                    pathF = false;
                    polygon.setPath(path2);
                } else {
                    pathF = true;
                    polygon.setPath(path1);
                }
            });
            //setStrokeColor()设置多边形覆盖物的边线颜色
            var strokeCF = true;
            var strokeC = document.getElementById("strokeC");
            qq.maps.event.addDomListener(strokeC, "click", function() {
                polygon.setMap(map);
                polygon.setVisible(true);
                if (strokeCF) {
                    strokeCF = false;
                    polygon.setStrokeColor("#1c29d8");
                } else {
                    strokeCF = true;
                    polygon.setStrokeColor("#000");
                }
            });
            //setStrokeDashStyle()设置多边形覆盖物的边线样式
            var dashF = true;
            var dash = document.getElementById("dash");
            qq.maps.event.addDomListener(dash, "click", function() {
                polygon.setMap(map);
                polygon.setVisible(true);
                if (dashF) {
                    dashF = false;
                    polygon.setStrokeDashStyle("dash");
                } else {
                    dashF = true;
                    polygon.setStrokeDashStyle("solid");
                }
            });
            //setStrokeWeight()设置多边形覆盖物的边线宽度
            var strokeWeightF = true;
            var strokeWeight = document.getElementById("strokeWeight");
            qq.maps.event.addDomListener(strokeWeight, "click", function() {
                polygon.setMap(map);
                polygon.setVisible(true);
                if (strokeWeightF) {
                    strokeWeightF = false;
                    polygon.setStrokeWeight(10);
                } else {
                    strokeWeightF = true;
                    polygon.setStrokeWeight(5);
                }
            });
            //setVisible()设置多边形覆盖物的可见性
            var visibleTF = true;
            var visibleT = document.getElementById("visibleT");
            qq.maps.event.addDomListener(visibleT, "click", function() {
                polygon.setMap(map);
                if (visibleTF) {
                    visibleTF = false;
                    polygon.setVisible(false);
                } else {
                    visibleTF = true;
                    polygon.setVisible(true);
                }
            });
        }
    </script>
</head>

<body onload="init()">
    <div style="width:603px;height:300px" id="container"></div>
    <div id="info">
        <button id="mapM" class="btn">setMap</button>
        <button id="cursor" class="btn">setCursor</button>
        <button id="fillC" class="btn">setFillColor</button>
        <button id="path" class="btn">setPath</button>
        <button id="strokeC" class="btn">setStrokeColor</button>
        <button id="dash" class="btn">setStrokeDashStyle</button>
        <button id="strokeWeight" class="btn">setStrokeWeight</button>
        <button id="visibleT" class="btn">setVisible</button>
    </div>
</body>

</html>