[高德地图API]学习笔记(一)常见问题记录

Q1 用鼠标工具绘制覆盖物,如何获取覆盖物的路径?(官方记录问题)

参考链接:https://lbs.amap.com/faq/js-api/map-js-api/cover/43364

代码示例

<!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">
    <style>
        html,
        body,
        #container {
            width: 100%;
            height: 100%;
        }
    </style>
    <title>椭圆的绘制和编辑</title>
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"/>
    <script src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值&plugin=AMap.MouseTool"></script>
    <script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
</head>
<body>
<div id="container"></div>
<div class="input-card" style="width: 200px">
    <h4 style="margin-bottom: 10px; font-weight: 600">利用mouseTool绘制覆盖物</h4>
    <button class="btn" onclick="drawPolyline()" style="margin-bottom: 5px">绘制线段</button>
    <button class="btn" onclick="drawPolygon()" style="margin-bottom: 5px">绘制多边形</button>
    <button class="btn" onclick="drawRectangle()" style="margin-bottom: 5px">绘制矩形</button>
    <button class="btn" onclick="drawCircle()" style="margin-bottom: 5px">绘制圆形</button>
</div>
<script type="text/javascript">
    var map = new AMap.Map("container", {
        center: [107.943579, 30.131735],
        zoom: 14
    });

    var mouseTool = new AMap.MouseTool(map)

    function drawPolyline() {
        mouseTool.polyline({
            strokeColor: "#3366FF",
            strokeOpacity: 1,
            strokeWeight: 6,
            // 线样式还支持 'dashed'
            strokeStyle: "solid",
            // strokeStyle是dashed时有效
            // strokeDasharray: [10, 5],
        })
    }

    function drawPolygon() {
        mouseTool.polygon({
            strokeColor: "#FF33FF",
            strokeOpacity: 1,
            strokeWeight: 6,
            strokeOpacity: 0.2,
            fillColor: '#1791fc',
            fillOpacity: 0.4,
            // 线样式还支持 'dashed'
            strokeStyle: "solid",
            // strokeStyle是dashed时有效
            // strokeDasharray: [30,10],
        })
    }

    function drawRectangle() {
        mouseTool.rectangle({
            strokeColor: 'red',
            strokeOpacity: 0.5,
            strokeWeight: 6,
            fillColor: 'blue',
            fillOpacity: 0.5,
            // strokeStyle还支持 solid
            strokeStyle: 'solid',
            // strokeDasharray: [30,10],
        })
    }

    function drawCircle() {
        mouseTool.circle({
            strokeColor: "#FF33FF",
            strokeOpacity: 1,
            strokeWeight: 6,
            strokeOpacity: 0.2,
            fillColor: '#1791fc',
            fillOpacity: 0.4,
            strokeStyle: 'solid',
            // 线样式还支持 'dashed'
            // strokeDasharray: [30,10],
        })
    }

    mouseTool.on('draw', function (event) {
        // event.obj 为绘制出来的覆盖物对象
        log.info('覆盖物对象绘制完成')
        alert('是否提交')
    })

    AMap.event.addListener( mouseTool,'draw',function(e){
        console.log(e.obj.getPath());//获取路径/范围
        console.log(e.obj.getArea());
    });

</script>
</body>
</html>

Q2 编辑多边形后如何获取多边形的路径?(官方记录问题)

参考链接:https://lbs.amap.com/faq/js-api/map-js-api/cover/43418

代码示例:

<!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="http://cache.amap.com/lbs/static/main1119.css"/>
    <script src="http://webapi.amap.com/maps?v=1.3&key=您申请的key值&plugin=AMap.PolyEditor,AMap.CircleEditor"></script>
    <script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
</head>

<body>
<div id="container"></div>
<div class="button-group">
 <input type="button" class="button" value="开始编辑多边形" onClick="editor.startEditPolygon()"/>
    <input type="button" class="button" value="结束编辑多边形" onClick="editor.closeEditPolygon()"/>
</div>

<script>
    var editorTool, map = new AMap.Map("container", {
        resizeEnable: true,
        center: [116.403322, 39.900255],//地图中心点
        zoom: 13 //地图显示的缩放级别
    });

    //在地图上绘制折线
    var editor={};
    editor._polygon=(function(){

        var arr = [ //构建多边形经纬度坐标数组
        [116.403322,39.920255],
        [116.410703,39.897555],
        [116.402292,39.892353],
        [116.389846,39.891365]
        ]

        return new AMap.Polygon({
            map: map,
            path: arr,
            strokeColor: "#0000ff",
            strokeOpacity: 1,
            strokeWeight: 3,
            fillColor: "#f5deb3",
            fillOpacity: 0.35
        });
    })();

    map.setFitView();
    editor._polygonEditor= new AMap.PolyEditor(map, editor._polygon);
    editor.startEditPolygon=function(){
        editor._polygonEditor.open();
    }
    editor.closeEditPolygon=function(){
        editor._polygonEditor.close();
        var a =  editor._polygon.getPath();
        console.log(a);
    }
</script>
</body>
</html>

Q3 清除当前所绘制的图形?

参考链接:https://lbs.amap.com/api/javascript-api/example/mouse-operate-map/mouse-draw-overlayers

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值