web端学习

高德地图 闪烁圆形标记

2018-12-28  本文已影响792人  不见当年三月花
image.png

全国地图上图标闪烁 基于经纬度

<script type="text/javascript"
        src="https://webapi.amap.com/maps?v=1.4.11&key=你的AK"></script>
<script src="//webapi.amap.com/ui/1.0/main.js?v=1.0.11"></script>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>默认点标记</title>
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"/>
    <style>
        html, body, #container {
            height: 100%;
            width: 100%;
        }

        .amap-icon img,
        .amap-marker-content img{
            width: 25px;
            height: 34px;
        }

        .marker {
            position: absolute;
            top: -20px;
            right: -118px;
            color: #fff;
            padding: 4px 10px;
            box-shadow: 1px 1px 1px rgba(10, 10, 10, .2);
            white-space: nowrap;
            font-size: 12px;
            font-family: "";
            background-color: #25A5F7;
            border-radius: 3px;
        }

        .input-card{
            width: 32rem;
            z-index: 170;
        }

        .input-card .btn{
            margin-right: .8rem;
        }

        .input-card .btn:last-child{
            margin-right: 0;
        }
    </style>
</head>
<body>
<div id="container"></div>
<div class="input-card">
    <label style="color:grey">点标记操作</label>
    <div class="input-item">
        <input id="addMarker" type="button" class="btn" onclick="addMarker()" value="添加点标记覆盖物">
        <input id="updateMarker" type="button" class="btn" onclick="updateMarker()" value="更新点标记覆盖物">
        <input id="clearMarker" type="button" class="btn" onclick="clearMarker()" value="删除点标记覆盖物">
    </div>
</div>
<script type="text/javascript"
        src="https://webapi.amap.com/maps?v=1.4.11&key=你的AK"></script>
<script src="//webapi.amap.com/ui/1.0/main.js?v=1.0.11"></script>
<script type="text/javascript">
    var marker, map = new AMap.Map("container", {
        mapStyle: 'amap://styles/dark',
        resizeEnable: true,
        center: [113.23, 23.16  ],
        zoom: 11
    });

    // 实例化点标记
    function addMarker() {
        marker = new AMap.Marker({
            icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
            position: [113.231, 23.1116 ],
            offset: new AMap.Pixel(-13, -30),
            period:50
        });
        marker.setMap(map);
    }

    function updateMarker() {

        if (!marker) {
            return;
        }

        // 自定义点标记内容
        var markerContent = document.createElement("div");

        // 点标记中的图标
        var markerImg = document.createElement("img");
        markerImg.className = "markerlnglat";
        markerImg.src = "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-red.png";
        markerContent.appendChild(markerImg);

        // 点标记中的文本
        var markerSpan = document.createElement("span");
        markerSpan.className = 'marker';
        markerSpan.innerHTML = "Hi,我被更新啦!";
        markerContent.appendChild(markerSpan);

        marker.setContent(markerContent); //更新点标记内容
        marker.setPosition([116.391467, 39.927761]); //更新点标记位置
    }

    // 清除 marker
    function clearMarker() {

        if (marker) {
            marker.setMap(null);
            marker = null;
        }
    }




    //just some colors
    var colors = [
        '#0cc2f2',
        '#4fd2b1',
        '#90e36f',
        '#ffe700',
        '#ff9e00',
        '#ff6700',
        '#ff1800'
    ];

    AMapUI.load(['ui/misc/PointSimplifier', 'lib/$'], function(PointSimplifier, $) {

        if (!PointSimplifier.supportCanvas) {
            alert('当前环境不支持 Canvas!');
            return;
        }

        var pointSimplifierIns = new PointSimplifier({
            zIndex: 115,
            autoSetFitView: false,
            map: map, //所属的地图实例

            getPosition: function(item) {
                if (!item) {
                    return null;
                }

                var parts = item.split(',');

                return [parseFloat(parts[0]), parseFloat(parts[1])];
            },
            getHoverTitle: function(dataItem, idx) {
                return '序号: ' + idx;
            },
            //使用GroupStyleRender
            renderConstructor: PointSimplifier.Render.Canvas.GroupStyleRender,
            renderOptions: {
                eventSupport: false, //禁止事件,对性能更友好
                //点的样式
                pointStyle: {
                    fillStyle: null,
                    width: 5,
                    height: 5
                },
                topNAreaStyle: null,
                getGroupId: function(item, idx) {

                    //随机返回一个组ID
                    return Math.ceil(colors.length * Math.random());
                },
                groupStyleOptions: function(gid) {

                    //随机设置大小
                    var radius = 2 + 10 * Math.random();

                    return {

                        pointStyle: radius > 0 ? {
                            content: function(ctx, x, y, width, height) {

                                var p = {
                                    x: x + width / 2,
                                    y: y + height / 2,
                                    radius: radius
                                };

                                ctx.beginPath();
                                var gradient = ctx.createRadialGradient(p.x, p.y, 0, p.x, p.y, p.radius);
                                gradient.addColorStop(0, "rgba(111,120,249,0.8)");
                                gradient.addColorStop(1, "rgba(222,120,249,0.1)");
                                ctx.fillStyle = gradient;
                                ctx.arc(p.x, p.y, p.radius, Math.PI * 2, false);
                                ctx.fill();

                            },

                            //fillStyle: colors[gid % colors.length],
                            width: radius * 2,
                            height: radius * 2
                        } : null,
                        pointHardcoreStyle: {
                            width: radius * 2 + 3,
                            height: radius * 2 + 3
                        }
                    };
                }

            }
        });


        //重复render
        setInterval(function() {
            pointSimplifierIns.render();
        }, 500)


        $('<div id="loadingTip">加载数据,请稍候...</div>').appendTo(document.body);
        $.get('https://a.amap.com/amap-ui/static/data/10w.txt', function(csv) {

            var data = csv.split('\n');
            /**
             * 加载数据 直接后台http生成  可以查看这个txt的文件格式
             * 我分成三个加载 刚好三种颜色
             * */
           // pointSimplifierIns.setData(data.slice(0, 5000));

            pointSimplifierIns1.setData(data.slice(0, 5000));

            $('#loadingTip').remove();
        });



        //重复render
        setInterval(function() {
            pointSimplifierIns1.render();
        }, 500)


        /*pointSimplifierIns1
        * 作为一个全局的闪烁的标志
        * 一个颜色我实例化一个
        * 只是为了方便
        * 读者可以自己优化
        * */
        var pointSimplifierIns1 = new PointSimplifier({
            zIndex: 115,
            autoSetFitView: false,
            map: map, //所属的地图实例

            getPosition: function(item) {
                if (!item) {
                    return null;
                }

                var parts = item.split(',');

                return [parseFloat(parts[0]), parseFloat(parts[1])];
            },
            getHoverTitle: function(dataItem, idx) {
                return '序号: ' + idx;
            },
            //使用GroupStyleRender
            renderConstructor: PointSimplifier.Render.Canvas.GroupStyleRender,
            renderOptions: {
                eventSupport: false, //禁止事件,对性能更友好
                //点的样式
                pointStyle: {
                    fillStyle: null,
                    width: 5,
                    height: 5
                },
                topNAreaStyle: null,
                getGroupId: function(item, idx) {

                    //随机返回一个组ID
                    return Math.ceil(colors.length * Math.random());
                },
                groupStyleOptions: function(gid) {

                    //随机设置大小
                    var radius = 2 + 10 * Math.random();

                    return {

                        pointStyle: radius > 0 ? {
                            content: function(ctx, x, y, width, height) {

                                var p = {
                                    x: x + width / 2,
                                    y: y + height / 2,
                                    radius: radius
                                };

                                ctx.beginPath();
                                var gradient = ctx.createRadialGradient(p.x, p.y, 0, p.x, p.y, p.radius);
                               // gradient.addColorStop(0, "rgba(0,238,118,0.8)");
                                // gradient.addColorStop(1, "rgba(0,238,110,0.1)");
                                //这里控制图标闪烁颜色 rgb
                                gradient.addColorStop(0, "rgba(255,255,0,0.8)");
                                gradient.addColorStop(1, "rgba(255,255,0,0.1)");
                                ctx.fillStyle = gradient;
                                ctx.arc(p.x, p.y, p.radius, Math.PI * 2, false);
                                ctx.fill();

                            },

                            //fillStyle: colors[gid % colors.length],
                            width: radius * 2,
                            height: radius * 2
                        } : null,
                        pointHardcoreStyle: {
                            width: radius * 2 + 3,
                            height: radius * 2 + 3
                        }
                    };
                }

            }
        });

    });


</script>
</body>
</html>
上一篇下一篇

猜你喜欢

热点阅读