高德地图 JS API示例->几何计算->距离/面积计算-> 路径长度

高德地图 JS API示例->几何计算->距离/面积计算-> 路径长度

使用 AMap.GeometryUtil.distanceOfLine 计算某段路径的实际长度,返回数据以米为单位。

<!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" type="text/css">
    <style>
      html,body,#container{
        height: 100%
      }
    </style>
</head>
<body>
<div id="container"></div>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script>
<script type="text/javascript">
    //初始化地图对象,加载地图
    var map = new AMap.Map("container", {
        resizeEnable: true,
        zoom: 13
    });
   
    function pathLength(){ 
        var arr=new Array();//经纬度坐标数组 
        arr.push(new AMap.LngLat("116.368904","39.913423")); 
        arr.push(new AMap.LngLat("116.382122","39.901176")); 
        arr.push(new AMap.LngLat("116.387271","39.912501")); 
        arr.push(new AMap.LngLat("116.398258","39.904600")); 
        arr.push(new AMap.LngLat("116.427097","39.912500"));
        
        //定义折线对象
        polyline=new AMap.Polyline({
            path:arr,     //设置折线的节点数组
            strokeColor:"red",
            strokeOpacity:1,
            strokeWeight:3,
            strokeDasharray:[10,5]
        }); 
        
        polyline.setMap(map);//地图上添加折线 
        
        var distance = Math.round(AMap.GeometryUtil.distanceOfLine(arr));
        var text = new AMap.Text({
            position: new AMap.LngLat(116.427097,39.912500),
            text: '折线长' + distance + '米',
            offset: new AMap.Pixel(-20, -20)
        })
        map.add(text);
        map.setFitView();
    }

    pathLength();

</script>
</body>
</html>
0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x