高德地图 判断点是否在多边形内

uniapp yekong

在uniapp项目开发中,需要获取当前的位置,并绘制接口返回位置信息为一个多边形,判断当前位置是否在多边形内,这用到了高德地图js api
使用 AMap.GeometryUtil.isPointInRing ,判断点是否在多边形内。
高德地图 判断点是否在多边形内

<script type="text/javascript">
    //初始化地图对象,加载地图
    var map = new AMap.Map("container", {
        resizeEnable: true,
        zoom: 13
    });

    // 创建多边形
    var path = [
        [116.169465,39.932670],
        [116.160260,39.924492], 
        [116.186138,39.879817], 
        [116.150625,39.710019],
        [116.183198,39.709920], 
        [116.226950,39.777616], 
        [116.421078,39.810771],
        [116.442621,39.799892],
        [116.463478,39.790066], 
        [116.588276,39.809551],
        [116.536091,39.808859],
        [116.573856,39.839643], 
        [116.706380,39.916740],
        [116.657285,39.934545],
        [116.600293,39.937770],
        [116.540039,39.937968],
        [116.514805,39.982375],
        [116.499935,40.013710],
        [116.546520,40.030443], 
        [116.687668,40.129961], 
        [116.539697,40.080659],
        [116.503390,40.058474],
        [116.468800,40.052578]
    ];
    var polygon = new AMap.Polygon({
        map: map,
        fillOpacity:0.4,
        path: path
    });

    // 创建点
    var marker = new AMap.Marker({
        map: map,
        draggable:true,
        position: [116.566298, 40.014179]
    });

    function compute(){
        var point = marker.getPosition();
        var isPointInRing = AMap.GeometryUtil.isPointInRing(point,path);
        marker.setLabel({
            content:isPointInRing?'内部':'外部',
            offset:new AMap.Pixel(20,0)
        });
    }

    compute();
    // 为marker绑定拖拽事件
    marker.on('dragging',compute)
    map.setFitView();
   
</script>

高德地图示例地址

判断点是否在多边形内

uniapp 使用高德地图js api实例

uniapp app高德地图

喜欢