关于地图添加大量marker点进行卡顿问题

自己在百度api 调试界面 3.0 的进行调试就行 ,确实不卡顿。并且事件都有

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>CanvasLayer</title>
<script type="text/javascript" src="//api.map.baidu.com/api?v=3.0&ak=您的密钥"></script>
<style type="text/css">
body, html,#container {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
</style>
</head>
<body>
    <div id="container"></div>
</body>
</html>
<script type="text/javascript">
    var mp = new BMap.Map("container");
    mp.centerAndZoom(new BMap.Point(116.3964,39.9093), 10);
    mp.enableScrollWheelZoom();

    var canvasLayer = new BMap.CanvasLayer({
        update: update
    });

    function update() {
        var ctx = this.canvas.getContext("2d");

        if (!ctx) {
            return;
        }

        ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);

        var temp = {};
        ctx.fillStyle = "rgba(50, 50, 255, 0.7)";
        ctx.beginPath();
        var data = [
            new BMap.Point(116.297047,39.979542),
            new BMap.Point(116.321768,39.88748),
            new BMap.Point(116.494243,39.956539)
        ];
      for(var j = 0 ; j<100000;j++){
        for (var i = 0, len = data.length; i < len; i++) {
            var pixel = mp.pointToPixel(data[i]);
            ctx.fillRect(pixel.x, pixel.y, 30, 30);
        }
        }
    }
    mp.addOverlay(canvasLayer);
</script>

这是用了CanvasLayer这个api 有不明白的搜 jsapi3类参考一看

下面是另一种方法 此方法也是用canvas 但是这个用的被封装过的,即zrender 可以自己下载使用
一点都不卡,

// option包含以下参数
// data:点的数组
// iconUrl:自定义点的图片路径
// matchingMethod: icon匹配方法
// click:点击事件(非必填)
// mousemove:移入事件(非必填)
// mouseout:移出事件(非必填)
function addMapPoint(map,option){
	var zr = null;
	var canvasLayer = new BMap.CanvasLayer({
		update: update
	});
	var zoom = map.getZoom()
	map.addOverlay(canvasLayer);
	function update(){
		zr = zrender.init(this.canvas); //初始化zrender
		zr.resize()
		option.data.forEach(item => {
			var point = map.pointToPixel( new BMap.Point(Number(item.lng),Number(item.lat)));
			var circle = new zrender.Image({
				style: {
					image: option.iconUrl?option.iconUrl:option.matchingMethod(item),
					x: point.x - zoom,
					y: point.y - zoom,
					width: zoom*2,
					height: zoom*2
				},
				data:item,
			});
			if(option.click){
				circle.on('click',option.click)
			}
			if(option.mousemove){
				circle.on('mousemove',option.mousemove)
			}
			if(option.mouseout){
				circle.on('mouseout',option.mouseout)
			}
			zr.add(circle);
		})
	}
	return canvasLayer;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在高德地图添加 marker ,可以按照以下步骤进行: 1. 引入高德地图 JavaScript API 库 在 HTML 页面中引入高德地图 JavaScript API 库,可以在官网上下载或使用 CDN 引入。 2. 创建地图 使用 `new AMap.Map()` 方法创建一个地图实例,指定地图容器的 ID 和地图的中心和缩放级别。 ``` var map = new AMap.Map('mapContainer', { center: [116.397428, 39.90923], zoom: 13 }); ``` 3. 创建 marker 使用 `new AMap.Marker()` 方法创建一个 marker ,指定 marker 的位置和其他属性。 ``` var marker = new AMap.Marker({ position: [116.397428, 39.90923], title: '这是一个 marker ', icon: 'https://webapi.amap.com/images/0.png' }); ``` 4. 将 marker 添加地图中 使用 `map.add(marker)` 方法将 marker 添加地图中。 ``` map.add(marker); ``` 完整的代码示例: ``` <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>高德地图添加 marker </title> <script src="https://webapi.amap.com/maps?v=1.4.15&key=你的高德地图 API Key"></script> <style type="text/css"> #mapContainer { width: 100%; height: 500px; } </style> </head> <body> <div id="mapContainer"></div> <script type="text/javascript"> var map = new AMap.Map('mapContainer', { center: [116.397428, 39.90923], zoom: 13 }); var marker = new AMap.Marker({ position: [116.397428, 39.90923], title: '这是一个 marker ', icon: 'https://webapi.amap.com/images/0.png' }); map.add(marker); </script> </body> </html> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值