• 【高德地图】 覆盖物/画点/画折线/画多边形/画矩形/画圆


    在这里插入图片描述

    官方示例

    https://lbs.amap.com/demo/javascript-api/example/mouse-operate-map/mouse-draw-overlayers

    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;
          }
        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 type="radio" name='func' checked="" value='marker'><span class="input-text">画点span>
              <input type="radio" name='func' value='polyline'><span class="input-text">画折线span>
              <input type="radio" name='func' value='polygon'><span class="input-text" style='width:5rem;'>画多边形span>
            div>
            <div class="input-item">
              <input type="radio" name='func' value='rectangle'><span class="input-text">画矩形span>
              <input type="radio" name='func' value='circle'><span class="input-text">画圆span>
            div>
            <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',{
            zoom:14
        });
    
        var mouseTool = new AMap.MouseTool(map); 
        //监听draw事件可获取画好的覆盖物
        var overlays = [];
        mouseTool.on('draw',function(e){
            overlays.push(e.obj);
        }) 
        
        function draw(type){
          switch(type){
            case 'marker':{
                mouseTool.marker({
                  //同Marker的Option设置
                });
                break;
            }
            case 'polyline':{
                mouseTool.polyline({
                  strokeColor:'#80d8ff'
                  //同Polyline的Option设置
                });
                break;
            }
            case 'polygon':{
                mouseTool.polygon({
                  fillColor:'#00b0ff',
                  strokeColor:'#80d8ff'
                  //同Polygon的Option设置
                });
                break;
            }
            case 'rectangle':{
                mouseTool.rectangle({
                  fillColor:'#00b0ff',
                  strokeColor:'#80d8ff'
                  //同Polygon的Option设置
                });
                break;
            }
            case 'circle':{
                mouseTool.circle({
                  fillColor:'#00b0ff',
                  strokeColor:'#80d8ff'
                  //同Circle的Option设置
                });
                break;
            }
          }
        }
        var radios = document.getElementsByName('func');
        for(var i=0;i<radios.length;i+=1){
            radios[i].onchange = function(e){
    
              draw(e.target.value)
            }
        }
        draw('marker')
    
        document.getElementById('clear').onclick = function(){
            map.remove(overlays)
            overlays = [];
        }  
        document.getElementById('close').onclick = function(){
            mouseTool.close(true)//关闭,并清除覆盖物
            for(var i=0;i<radios.length;i+=1){
                radios[i].checked = false;
            }
        }
        script>
      body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119

    应用

            <div style="margin-top: 10px;" class="button-item" @click="handleShowLine"
              :id="mapChange === 3 ? 'line1' : 'line'">
            div>
            <div style="margin-top: 10px;" class="button-item" @click="handleShowCircle"
              :id="mapChange === 4 ? 'circle1' : 'circle'">
            div>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    // type 画的类型
        linePolygon(type) {
        // 创建MouseTool实例
          var mouseTool = new AMap.MouseTool(map);
          console.log(mouseTool, 'mouseTool');
          let that = this
    
          // 鼠标在地图上画了之后获取画的图形的坐标,在这之前需要实例化mouseTool.polygon()方法
          this.AMap.Event.addListener(mouseTool, 'draw', function (e) {
            // console.log(e, 'eee');
            let arr = e.obj.getPath();//获取坐标
            // console.log(arr, 'arr');
            let polyList = []
            let polyList1 = []
    
    
            polyList = arr.map((item, index) => {
              return {
                lat: item.lat,
                lon: item.lng
              }
            })
            // 格式转换
            polyList1 = arr.map((item, index) => {
              return [item.lng, item.lat]
            })
            // 画完多边形之后,可接口获取多边形内点
            queryP({}).then((res) => {
              console.log(res, 'resss');
              that.Ponit= res.data
              mouseTool.close(true)//关闭,并清除覆盖物
    
    
          // [【高德地图】根据经纬度多边形的绘制(可绘制区域以及任意图形)](https://blog.csdn.net/Xiang_Gong_Ya_/article/details/132775837?csdn_share_tail=%7B%22type%22:%22blog%22,%22rType%22:%22article%22,%22rId%22:%22132775837%22,%22source%22:%22Xiang_Gong_Ya_%22%7D)
    
              document.getElementById('close').onclick = function () {
                mouseTool.close(true)//关闭,并清除覆盖物
                console.log(111);
                // 清空地图内容
                window.map.clearMap()
              }
            })
          });
    
          function draw(type) {
            switch (type) {
              case 'line': {
                mouseTool.polygon({
                  fillColor: '#00b0ff',
                  strokeColor: '#80d8ff'
                  //同Polygon的Option设置
                });
                break;
              }
              case 'circle': {
                mouseTool.circle({
                  fillColor: '#00b0ff',
                  strokeColor: '#80d8ff'
                  //同Circle的Option设置
                });
                break;
              }
            }
          }
          draw(type)
          
          // 清除
           document.getElementById('clear').onclick = function () {
             window.map.remove(overlays)
             overlays = [];
           }
    
        },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
  • 相关阅读:
    服务器从上架到上线经验
    《算法通关村——双指针妙用》
    1440_TC275 DataSheet阅读笔记1
    DOM网页特效的说明》
    Docker+Nginx部署前后端分离项目(SpringBoot+Vue)的详细教程
    江门晚造粮食(水稻)扩种1万亩 国稻种芯:“以晚补早”夺丰收
    Python从入门到精通秘籍八
    OSPF被动接口配置(华为)
    GaussDB数据库SQL系列-LOCK TABLE
    GCC编译宏_GLIBCXX_USE_CXX11_ABI背景分析和实现原理
  • 原文地址:https://blog.csdn.net/Xiang_Gong_Ya_/article/details/132775693