通过点击来绘制 左键选择点,三点一个面,右键 确定绘制 ,

		<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="chrome=1">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" type="text/css">
    <style>
      html,body,#container{
        height: 100%
      }
      .input-item{
        height: 2.2rem;
      }
      .btn{
        width: 6rem;
        /* margin: 0 1rem 0 2rem; */
      }
      .input-text{
        width: 4rem;
        margin-right:1rem;
      }
 
        .amap-marker-label{
            border: 0;
            background-color: transparent;
        }
	  .info{
            position: absolute;
            top: 0;
            left: 0;
            min-width: 0;
			text-align: center;
        }
    </style>
    <title>高德框选</title>
  </head>
  <body>
    <div id='container'></div>
    <div class='info'>通过点击来绘制</div>
    <div class="input-card" style='width: 24rem;'>
      
        <div class="input-item">
            <input id="clear" type="button" class="btn" value="清除" />
			<input id="close" type="button" class="btn" value="关闭绘图" />
        </div>
    </div>
    <script src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值&plugin=AMap.MouseTool"></script>
    <script type="text/javascript">
    var map = new AMap.Map('container',{
		resizeEnable: true, //是否监控地图容器尺寸变化
		zoom: 14, //初始地图级别
		center: [126.947813, 44.410906], //初始地图中心点
		showIndoorMap: false //关闭室内地图
    });
	//map.setCity('舒兰市');
	// console.log(map.getCenter())
 // 创建点
    var marker = new AMap.Marker({
        map: map,
       // draggable:true,
        position: [126.947962, 44.406889],
		extData:{
			id:1,
			name:'茉莉香鸭颈王(舒兰店)'
			}
    });
	var marker1 = new AMap.Marker({
        map: map,
       // draggable:true,
        position: [126.950275, 44.411781],
		extData:{
			id:2,
			name:'王老六包子铺'
			}
    });
	var marker2 = new AMap.Marker({
        map: map,
       // draggable:true,
        position: [126.958212, 44.411545],
		extData:{
			id:3,
			name:'舒兰市满盆龙虾饭店'
			}
    });
	var marker3 = new AMap.Marker({
        map: map,
       // draggable:true,
        position: [126.949713, 44.411858],
		extData:{
			id:4,
			name:'皇妈厨房黄焖鷄米饭'
			}
    });
	var marker4 = new AMap.Marker({
        map: map,
       // draggable:true,
        position: [126.943696, 44.417337],
		extData:{   //框选后可以获取的值
			id:5,
			name:'聚财馋嘴鸭(正阳)'
			}
    });
    var mouseTool = new AMap.MouseTool(map); 
    var overlays = [];
    mouseTool.on('draw',function(e){
		map.remove(overlays)
        overlays = [];
		//debugger
        overlays.push(e.obj);
		var bounds = e.obj.getBounds();
		var allMarkers = map.getAllOverlays('marker');
     
		var overlayPath = [];	//覆盖物路径
	   if(e.obj.CLASS_NAME=="AMap.Marker"){
			overlayPath.push(e.obj.getPosition());
	   }else{
		   var southWest = bounds.getSouthWest();
		   var northEast = bounds.getNorthEast();
		   if(southWest.equals(northEast)){
				return;
		   }
			overlayPath = e.obj.getPath();
	   }
		var inner =[]  //框选内部的
		mouseTool.close(false);
		 for (let i = 0; i < allMarkers.length; i++) {
		  var point = allMarkers[i].getPosition();
		  var isPointInRing = AMap.GeometryUtil.isPointInRing(
			point,
			overlayPath
		  );
		  if (isPointInRing) {    //判断条件可以换成e.obj.contains(point)
			inner.push(allMarkers[i].getExtData());
		  }
		}
		console.log(inner)
    }) 
	
	 mouseTool.polygon({
	   fillColor:'#00b0ff',
	   strokeColor:'#80d8ff'
	   //同Polygon的Option设置
	 });
	
    document.getElementById('clear').onclick = function(){
        map.remove(overlays)
        overlays = [];
    }  
    document.getElementById('close').onclick = function(){
        mouseTool.close(true)//关闭,并清除覆盖物
		//map.clearLimitBounds();
      
    }
	
    </script>
  </body>
</html>
Logo

前往低代码交流专区

更多推荐