vue百度地图设置半径画圆圈地

<template>
    <div class="box-circle-map">
        <baidu-map ref="bCircleMap" class="map" :center="mapCenter" :zoom="15" :scroll-wheel-zoom="true" :inertial-dragging="false"
        :map-click="false" @ready="handler">
            <bm-view ref="viewMap" class="map"></bm-view>
            <bm-local-search :keyword="keyword" :nearby="circlePath" :auto-viewport="false" :panel="false"></bm-local-search>
            <bm-circle :center="circlePath.center" :radius="circlePath.radius" :stroke-opacity="0.5" fill-color="#39B1FC" :fill-opacity="0.3"
            :stroke-weight="2" stroke-color="#0000ff" @lineupdate="updateCirclePath" :editing="true"></bm-circle>
        </baidu-map>
        <div class="left-map-box">
            <el-input placeholder="请输入" v-model="keyword" clearable>
                <el-button class="name_search_icon" slot="append" icon="el-icon-search"></el-button>
            </el-input>
            <div class="condition-content">
                <div class="radius-range">半径范围</div>
                <div class="dis-flex-between radius-distance">
                    <div class="dis-flex-between">
                        <el-input placeholder="请输入" v-model="circlePath.radius" size="small" clearable></el-input>
                        <span class="distance-txt">米</span>
                    </div>
                    <span>以内</span>
                </div>

            </div>
        </div>
    </div>
</template>

<script>
export default {
    // name: "baiduMapCircle",
    data() {
        return {
            map: null,
            BMap: null,
            mapCenter: {lng: 113.269129, lat: 23.134538},
            position: [],
            circlePath: {
                center: {
                    lng: 113.269129,
                    lat: 23.134538
                },
                radius: 1000
            },
            keyword: "餐馆",
            overlayLabel: null,
        };
    },
    created(){
        // https://dafrok.github.io/vue-baidu-map/#/zh/overlay/circle
    },
    methods: {
        updateCirclePath(e) {
            let _self = this;
            let map = this.map;
            let BMap = this.BMap;
            let radius = e.target.getRadius();//获取圆半径(单位米,可利用BMapLib.DistanceTool工具自定义单位)
            let center = e.target.getCenter();//获取圆心坐标
            let bounds = e.target.getBounds().getNorthEast();//获取圆可视范围东北角坐标
            // 覆盖物的属性发生变化时触发
            this.circlePath.radius = e.target.getRadius();
            this.circlePath.center = e.target.getCenter();
            if(_self.overlayLabel != null){
                map.removeOverlay(_self.overlayLabel);
            }
            //生成Label覆盖层
            var point = new BMap.Point(bounds.lng, center.lat); 
            _self.overlayLabel = new BMap.Label("半径:"+ radius + "m",{offset:new BMap.Size(20,-10),position: point || e.target.rc[1].point});
            //添加覆盖层
            map.addOverlay(_self.overlayLabel);
        },
        // 鼠标绘图
        _mouseDrawingEvt(BMap, map){
            let _self = this;
            let styleOptions = {
                strokeColor:"#0000ff",    //边线颜色。
                fillColor:"#39B1FC",      //填充颜色。当参数为空时,圆形将没有填充效果。
                strokeWeight: 2,       //边线的宽度,以像素为单位。
                strokeOpacity: 0.5,       //边线透明度,取值范围0 - 1。
                fillOpacity: 0.3,      //填充的透明度,取值范围0 - 1。
                strokeStyle: 'solid' //边线的样式,solid或dashed。
            }
            //实例化鼠标绘制工具
            let drawingManager = new BMapLib.DrawingManager(map, {
                isOpen: false, //是否开启绘制模式
                enableDrawingTool: true, //是否显示工具栏
                drawingToolOptions: {
                    anchor: BMAP_ANCHOR_TOP_RIGHT, //位置
                    offset: new BMap.Size(100, 10), //偏离值
                    drawingModes: [BMAP_DRAWING_CIRCLE], // BMAP_DRAWING_CIRCLE 画圆
                },
                circleOptions: styleOptions, //圆的样式
            });

            //添加鼠标绘制工具监听事件,用于获取绘制结果
            drawingManager.addEventListener('circlecomplete', function(overlay) {
                _self.circlePath.center = overlay.getCenter();
                // 清除上一次绘制的图
                _self.map.removeOverlay(overlay);
                drawingManager.close();
            });
        },
        // 初始化地图
        handler ({BMap, map}) {
            this.BMap = BMap;
            this.map = map;
            this._mouseDrawingEvt(BMap, map)
        },
    },
    mounted(){
        let _self = this;
        this.$nextTick(() =>{
            if (_self.$refs.bCircleMap) {
                _self.$refs.bCircleMap.$el.style.height = (window.innerHeight - 132) + 'px';
            }
            if(_self.$refs.viewMap){
                _self.$refs.viewMap.$el.style.height = (window.innerHeight - 132) + 'px';
            }
            // 监听窗口大小变化
            window.addEventListener('resize', () => {
                if (_self.$refs.bCircleMap) {
                    _self.$refs.bCircleMap.$el.style.height = (window.innerHeight - 132) + 'px';
                }
                if(_self.$refs.viewMap){
                    _self.$refs.viewMap.$el.style.height = (window.innerHeight - 132) + 'px';
                }
            })
        })
    },
};
</script>

<style lang="scss" scoped>
.box-circle-map{
    position: relative;
    padding: 20px;
    .left-map-box{
        position: absolute;
        top: 40px;
        left: 40px;
        .name_search_icon{
            background: #39b1fc;
            border-color: #39b1fc;
            color: #fff;
            border-radius: 0 4px 4px 0;
            height: 40px;
        }
        .condition-content{
            background: #fff;
            padding: 20px;
            margin-top: 5px;
            color: #979797;
            .radius-range{
                height: 28px;
                line-height: 32px;
            }
            .radius-distance{
                .distance-txt{
                    margin: 0 5px;
                }
            }
        }
    }
}
</style>

 

posted @ 2020-06-01 10:30  日升月恒  阅读(2842)  评论(0)    收藏  举报