vue使用百度地图绘制多边形

vue使用百度地图绘制多边形

封装的一个组件DrawMap.vue

抛出属性:
pathss(默认绘制区域)
lat(纬度)lng(经度)【中心点】
scales(缩放大小)
is_Show(是否显示编辑按钮)

<style lang="less" scoped>
    .mapbox{
        width: 100%;
        height: 100%;
    }
</style>
<template>
   <div class="mapbox">
       <baidu-map
            :center="center"
            :zoom="zoom"
            :map-click="false"
            @mousemove="syncPolygon"
            @ready="handler"
            style="height:100%"
            @click="paintPolygon"
            @rightclick="newPolygon"
        >
        <bm-polygon
          :path="path"
          v-for="path of polygonPath.paths"
          :key="path.toString()"
          stroke-color="#FE8C73"
          fill-color="#FE886E"
          :fill-opacity="0.8"
          :stroke-opacity="0.5"
          :stroke-weight="2"
          @click="alertpath"
        />
        <bm-control>
          <button v-if="isShow" @click="toggle('polygonPath')">{{ polygonPath.editing ? '停止绘制' : '开始绘制' }}</button>
        </bm-control>
      </baidu-map>
    </div>
</template>
<script>
export default {
    props:['pathss','lat','lng','scales','is_Show'],
    name:'',
    data() {
        return {
            center: { lng: 116.412732, lat: 39.911707 },
            zoom: 13,
            polygonPath: {
                editing: false,
                paths: [
                    // [
                    //     {lat: '37.796849',lng: '112.611435'},
                    //     {lat: '37.792115',lng: '112.611004'},
                    //     {lat: '37.792229',lng: '112.62164'},
                    //     {lat: '37.79759',lng: '112.62164'}
                    // ],
                ] // 绘制完成后的经纬度,其实是在画的时候动态push的,因为在点击的时候触发了 paintPolygon 函数
            },
            //中心点
            clat:'37.796849',
            clng:'112.611435',
            // 缩放大小
            scale:15,
            //是否显示按钮
            isShow:false,
        }
    },
    created(){
        
        
    },
    mounted() {
        this.isShow=this.is_Show
       	this.polygonPath.paths.push(this.pathss)
        if(this.lat!=''&&this.lng!=''){
            this.clat=this.lat
            this.clng=this.lng
        }
        if(this.scales!=''){
            this.scale=this.scales
        }
    },
    methods: {
        handler ({ BMap, map }) {
            console.log(BMap, map)
            map.enableScrollWheelZoom()
            let a=new BMap.Point(this.clng,this.clat)
            map.centerAndZoom(a, this.scale)
        },
        getClickInfo (e) {
            console.log(e.point.lng)
            console.log(e.point.lat)
        },
        // 开启多边形绘制
        toggle (name) {
            this[name].editing = !this[name].editing
            // 在这里做一步判断,如果有路径且开启绘制就把原来的路径清空
            if (this.polygonPath.paths && this.polygonPath.editing) {
            this.polygonPath.paths = []
            }
        },
        // 鼠标移动时
        syncPolygon (e) {
            if (!this.polygonPath.editing) {
                return
            }
            const { paths } = this.polygonPath
            if (!paths.length) {
                return
            }
            const path = paths[paths.length - 1]
            if (!path.length) {
                return
            }
            if (path.length === 1) {
                path.push(e.point)
            }
            this.$set(path, path.length - 1, e.point)
        },
        // 鼠标左键点击时往路径里push一个点
        newPolygon () {
            if (!this.polygonPath.editing) {
                return
            }
            // 当开始绘制后把按钮调回开始绘制状态,防止绘制多个图形
            this['polygonPath'].editing = !this['polygonPath'].editing
            const { paths } = this.polygonPath
            if (!paths.length) {
                paths.push([])
            }
            const path = paths[paths.length - 1]
            path.pop()
            if (path.length) {
                paths.push([])
            }
            //右击结束
            console.log('右击结束')
        },
        // 鼠标右键多边形绘制完成
        paintPolygon (e) {
            if (!this.polygonPath.editing) {
                return
            }
            const { paths } = this.polygonPath
            !paths.length && paths.push([])
            paths[paths.length - 1].push(e.point)
        },
        alertpath (e) {
            console.log(e.currentTarget.so)
            //获取绘制的经纬度
            console.log(this.polygonPath.paths[0])
        }

    }
}
</script>
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不想写前端的前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值