ウィジットのz-indexを変更する

実行結果

次の例では、ウィジットのz-indexを変更して表示順を変更します。

Your borwser is not supporting object tag. Please use one of the latest browsers.
Go to ./apisample/widget/widget_025.html

ソースコードと解説

同じ種類のウィジットの表示順を変更するには、各ウィジットクラスのsetZindex()を利用します。デフォルトは100です。

<!DOCTYPE html>
<html>
<head>

<meta charset="UTF-8">
<script src="//test.api.its-mo.com/v3/loader?key=JSZddc5111626c8|Zo4wz&api=zdcmap.js,control.js,userwidget.js,shape.js&enc=UTF8" type="text/javascript"></script>
<script type="text/javascript">

    var map, mrk1, mrk2, msginfo1, msginfo2, pg, ov, widget1, widget2,
        lat = 35.6778614, lon = 139.7703167;

    function loadMap() {
        map = new ZDC.Map(
            document.getElementById('ZMap'),
            {
                latlon: new ZDC.LatLon(lat, lon),
                zoom: 4,
                mapType: ZDC.MAPTYPE_HIGHRES_LV18
            }
        );

        map.addWidget(new ZDC.ScaleBar());
        map.addWidget(new ZDC.Control());

        /* 多角形の頂点を作成 */
        var latlons = [];
        latlons.push(new ZDC.LatLon(35.70, 139.63));
        latlons.push(new ZDC.LatLon(35.70, 139.72));
        latlons.push(new ZDC.LatLon(35.66, 139.65));

        /* 線を作成。closePath:trueで多角形になる */
        pg = new ZDC.Polyline( latlons,
        {
            strokeColor: '#00FF00',
            strokeWeight: '2',
            fillColor: '#FF0000',
            closePath: true
        });

        /* 多角形を地図に追加 */
        map.addWidget(pg);

        /* 円を作成 */
        ov = new ZDC.Oval(
        {
            latlon: new ZDC.LatLon(35.66, 139.65),
            x: 2000,
            y: 2000
        },
        {
            strokeColor: '#00FF00',
            strokeWeight: '2',
            fillColor: '#FF0000',
            circle: true
        });

        /* 円を地図に追加 */
        map.addWidget(ov);

        /* マーカを作成 */
        mrk1 = new ZDC.Marker(new ZDC.LatLon(35.63, 139.71), {
            color: ZDC.MARKER_COLOR_ID_GRAY_L,
            number: ZDC.MARKER_NUMBER_ID_1_L
        });
        map.addWidget(mrk1);

        /* マーカを作成 */
        mrk2 = new ZDC.Marker(new ZDC.LatLon(35.6, 139.71), {
            color: ZDC.MARKER_COLOR_ID_YELLOW_L,
            number: ZDC.MARKER_NUMBER_ID_2_L
        });
        map.addWidget(mrk2);

        /* ユーザウィジットを表示 */
        var widget1label =
        {
            html: '<div style="background-color: #FFFFFF"><b><font color="#FF0000">Sample Widget1</font></b></div>',
            size: new ZDC.WH(110, 40)
        };
        widget1 = new ZDC.UserWidget(new ZDC.LatLon(35.66, 139.7), widget1label);
        map.addWidget(widget1);
        widget1.open();

        var widget2label =
        {
            html: '<div style="background-color: #FFFFFF"><b>Sample Widget2</b></div>',
            size: new ZDC.WH(110, 40)
        };
        widget2 = new ZDC.UserWidget(new ZDC.LatLon(35.67, 139.7), widget2label);
        map.addWidget(widget2);
        widget2.open();

        /* 吹き出しを表示 */
        msginfo1 = new ZDC.MsgInfo(new ZDC.LatLon(35.60, 139.95), {html: 'Sample MsgInfo1'});
        map.addWidget(msginfo1);
        msginfo1.open();

        /* 吹き出しを表示 */
        msginfo2 = new ZDC.MsgInfo(new ZDC.LatLon(35.54, 139.95), {html: 'Sample MsgInfo2'});
        map.addWidget(msginfo2);
        msginfo2.open();
    }

    /* マーカのz-indexを変更する */
    function chg_mrk_zindex(idx) {
        if (idx == 1) {
            mrk1.setZindex(101);
            mrk2.setZindex(100);
        } else {
            mrk1.setZindex(100);
            mrk2.setZindex(101);
        }
    }

    /* 吹き出しのz-indexを変更する */
    function chg_info_zindex(idx) {
        if (idx == 1) {
            msginfo1.setZindex(101);
            msginfo2.setZindex(100);
        } else {
            msginfo1.setZindex(100);
            msginfo2.setZindex(101);
        }
    }

    /* ユーザウィジットのz-indexを変更する */
    function chg_uwidget_zindex(idx) {
        if (idx == 1) {
            widget1.setZindex(101);
            widget2.setZindex(100);
        } else {
            widget1.setZindex(100);
            widget2.setZindex(101);
        }
    }

    /* 多角形と円のz-indexを変更する */
    function chg_draw_zindex(idx) {
        if (idx == 1) {
            pg.setZindex(101);
            ov.setZindex(100);
        } else {
            pg.setZindex(100);
            ov.setZindex(101);
        }
    }

</script>
</head>

<body onload="loadMap();">
    <div id="ZMap" style="border:1px solid #777777; width:750px; height:500px; top:0px; left:20px; position:absolute;"></div>
    <div id="IBox" style="top:510px; left:20px; position:absolute;">
        <input type="button" value="マーカ1を上にする" onclick="chg_mrk_zindex(1);"><br>
        <input type="button" value="マーカ2を上にする" onclick="chg_mrk_zindex(2);"><br>
        <input type="button" value="吹き出し1を上にする" onclick="chg_info_zindex(1);"><br>
        <input type="button" value="吹き出し2を上にする" onclick="chg_info_zindex(2);"><br>
        <input type="button" value="ユーザウィジット1を上にする" onclick="chg_uwidget_zindex(1);"><br>
        <input type="button" value="ユーザウィジット2を上にする" onclick="chg_uwidget_zindex(2);"><br>
        <input type="button" value="多角形を上にする" onclick="chg_draw_zindex(1);"><br>
        <input type="button" value="円を上にする" onclick="chg_draw_zindex(2);">
    </div>
</body>
</html>