vue & 百度地图:在地图上绘制多边形
<template>
<div class="hello">
<div style="margin-bottom:10px">
<button @click="clickStart('jinting')">设定坐标</button>
<button @click="clickEnd">退出设定</button>
<button @click="re" style="color:#ff0000">重置</button>
<button @click="getBoundary">生成区域</button>
<button @click="clickCenter">设定中心点</button>
<button @click="boo2 = !boo2">显示点集</button>
<input type="text" placeholder="请输入城市名称" width="200" v-model="cityName"></input>
</div>
<!-- map start -->
<div style="" id="dituContent" class="ditu-content"></div>
<!-- map end -->
<div style="text-align:center" class="ditu-point" v-if="savePointsArray.length > 0" v-show="boo2">
<p v-for="todo in savePointsArray">
<span style="padding-right:20px;">{{ todo.name }}</span><span>{{ todo.point }}</span>
</p>
</div>
</div>
</template> <script>
export default {
name: 'mymap',
data () {
return {
polygons: [],
clickCity: '',
polyline: null,
marker: null,
cityName: '',
status: 'none',
centerPoint: [],
savePointsArray: [],
savePointsString: '',
jinting: '',
todos: [
{ text: '学习 JavaScript' },
{ text: '学习 Vue' },
{ text: '整个牛项目' }
], mapObj:null,
boo2: false // 当点了显示点集的按钮之后变为true
}
},
mounted () {
this.initMap()
},
methods:{
re(){
this.polygons = []
this.clickCity = ''
this.polyline = null
this.marker = null
this.cityName = ''
this.status = 'none'
this.centerPoint = []
this.savePointsArray = []
this.savePointsString = ''
this.jinting = ''
this.mapObj = null
this.boo2 = false this.initMap()
},
initMap () {
this.createMap() //创建地图
},
createMap(){
let self = this
let m = new BMap.Map("dituContent")
let point = new BMap.Point(120.49,31.15)
m.centerAndZoom(point,12)
m.setCurrentCity("苏州")
m.addEventListener("click",function(e){
let ln = e.point.lng
let la = e.point.lat
self.clickFun(ln,la)
}); this.setMapEvent(m)
},
setMapEvent(m){
m.enableDragging();//启用地图拖拽事件,默认启用(可不写)
m.enableScrollWheelZoom();//启用地图滚轮放大缩小
m.enableDoubleClickZoom();//启用鼠标双击放大,默认启用(可不写)
m.enableKeyboard();//启用键盘上下左右键移动地图 this.addMapControl(m);//向地图添加控件
},
addMapControl(m){
//向地图中添加缩放控件
let ctrl_nav = new BMap.NavigationControl({anchor:BMAP_ANCHOR_TOP_LEFT,type:BMAP_NAVIGATION_CONTROL_LARGE});
m.addControl(ctrl_nav);
//向地图中添加缩略图控件
let ctrl_ove = new BMap.OverviewMapControl({anchor:BMAP_ANCHOR_BOTTOM_RIGHT,isOpen:1});
m.addControl(ctrl_ove);
//向地图中添加比例尺控件
let ctrl_sca = new BMap.ScaleControl({anchor:BMAP_ANCHOR_BOTTOM_LEFT});
m.addControl(ctrl_sca); this.mapObj = m
},
clickFun(ln,la){ if (this.status === 'inner') {
this.centerPoint[0] = ln
this.centerPoint[1] = la this.marker = new BMap.Marker(new BMap.Point(ln, la)); // 创建点
this.mapObj.addOverlay(this.marker);
} else if (this.clickCity === '') {
console.log('当前没有选择镇');
return
} else {
switch(this.clickCity)
{
case 'jinting':
this.jinting += (ln + ',' + la + ';')
this.pline(this.jinting)
// console.log('this.jinting', this.jinting)
break;
default:
return
}
}
},
clickStart(n){
$('#dituContent div').css({
'cursor':'crosshair'
})
this.polygons = []
this.clickCity = n
this.status = 'outside'
},
clickEnd(){
console.log('======================== CLICK END ======================')
this.clickCity = ''
this.status = 'none'
$('#dituContent div').css({
'cursor':'url("http://api0.map.bdimg.com/images/openhand.cur") 8 8, default'
})
},
clickCenter(){
this.status = 'inner'
$('#dituContent div').css({
'cursor':'crosshair'
})
},
pline(city){ this.mapObj.removeOverlay(this.polyline)
let pois = city.split(';')
let poisarray = []
pois.pop() for (let i = 0; i < pois.length; i++) {
poisarray.push(new BMap.Point(parseFloat(pois[i].split(',')[0]),parseFloat(pois[i].split(',')[1]))) } this.polyline = new BMap.Polyline(poisarray, {
enableEditing: false,//是否启用线编辑,默认为false
enableClicking: false,//是否响应点击事件,默认为true
strokeWeight:'5',//折线的宽度,以像素为单位
strokeOpacity: 0.5,//折线的透明度,取值范围0 - 1
strokeColor:"#18a45b" //折线颜色
});
this.mapObj.addOverlay(this.polyline); //增加折线
},
getBoundary(){
this.mapObj.removeOverlay(this.polyline)
let areas = []
let contentArray = []
areas.push(this.jinting)
let colors = "#fff492"
let pointArray = []
for (var i = 0; i < areas.length; i++) {
let ply = new BMap.Polygon(areas[i], { strokeWeight: 2, strokeColor: "#ff0000" }); //建立多边形覆盖物
ply.setFillColor(colors) //设置多边形的填充颜色
ply.setFillOpacity(0.35);
this.polygons.push(ply); //加入多边形数组,以之后获取多边形边界点集 // ply.enableEditing(); //范围可编辑 【编辑的时候开启】 this.mapObj.addOverlay(ply); //添加覆盖物
pointArray = pointArray.concat(ply.getPath());
} this.addlabel2()
this.savePoints()
this.reload2()
},
reload2(){
this.polygons = []
this.clickCity = ''
this.polyline = null
this.cityName = ''
this.jinting = ""
this.status = 'none'
this.mapObj.removeOverlay(this.marker)
this.centerPoint = []
},
addlabel2(){
let pointArray = [
new BMap.Point(this.centerPoint[0], this.centerPoint[1]),
]
let optsArray = [{}]
let labelArray = []
this.contentArray = [this.cityName]
for (let i = 0; i < pointArray.length; i++) {
optsArray[i].position = pointArray[i]
labelArray[i] = new BMap.Label(this.contentArray[i], optsArray[i])
labelArray[i].setStyle({
color: "red",
fontSize: "12px",
height: "20px",
lineHeight: "20px",
fontFamily: "微软雅黑"
});
this.mapObj.addOverlay(labelArray[i])
}
},
savePoints(){
console.log('=======================存储点集')
console.log(this.cityName)
if (this.cityName === ''){
alert('城市名称必填!')
return
}
if (this.savePointsCheck(this.cityName)){
alert('这个点已经存过了!')
return
}
let obj = {}
obj.name = this.cityName
obj.point = this.jinting.slice(0,-1)
obj.centerpoint = this.centerPoint[0] + ',' + this.centerPoint[1]
this.savePointsArray.push(obj)
console.log(this.savePointsArray) },
savePointsCheck(c) {
console.log(c)
let f = this.savePointsArray.find((v) => {
console.log(c)
console.log(v.name + '===' + c)
return v.name === c
}) return f
}
}
}
</script> <!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.ditu-content{
width:70%;
height:600px;
border:#ccc solid 1px;
margin:0 auto 20px;
}
.pointwords{
word-wrap: break-word;
text-align: center;
padding: 0 20px;
}
.button{
height: 100px;
}
</style>
最后效果如下:

参考:
https://blog.csdn.net/qq_38903950/article/details/78022174?locationNum=7&fps=1
https://www.cnblogs.com/RiseSoft/p/7979637.html
vue & 百度地图:在地图上绘制多边形的更多相关文章
- 软件项目技术点(7)——在canvas上绘制自定义图形
AxeSlide软件项目梳理 canvas绘图系列知识点整理 图形种类 目前我们软件可以绘制出来的形状有如下这几种,作为开发者我们一直想支持用户可以拖拽的类似word里面图形库,但目前还没有找到比 ...
- 利用百度API(JavaScript 版)实现在地图上绘制任一多边形,并判断给定经纬度是否在多边形范围内。以及两点间的测距功能
权声明:本文为博主原创文章,未经博主允许不得转载. 利用百度API(JavaScript 版)实现在地图上绘制任一多边形,并判断给定经纬度是否在多边形范围内.以及两点间的测距功能. 绘制多边形(蓝色) ...
- 记录开发基于百度地图API实现在地图上绘制轨迹并拾取轨迹对应经纬度的工具说明
前言: 最近一直在做数据可视化方面的工作,其中平面可视化没什么难度,毕竟已经有很多成熟的可供使用的框架,比如百度的echart.js,highcharts.js等.还有就是3D可视化了,整体来说难度也 ...
- Vue:如何在地图上添加自定义覆盖物(点)
目录 如何在地图上添加自定义覆盖物(点) 首发日期:2019-1-25 如何在地图上添加自定义覆盖物(点) 此文重点是在地图上标点,所以就省去引入百度地图的步骤了. 先给一下最终的效果. 这个效果主要 ...
- 在谷歌地图上绘制行政区域轮廓【结合高德地图的API】
实现思路: 1.利用高德地图行政区域API获得坐标列表 2.将坐标列表绘制在谷歌地图上[因为高德地图和国内的谷歌地图都是采用GCJ02坐标系,所有误差很小,可以不进行坐标误差转换] 注意点: 1.用百 ...
- GMap.Net开发之在地图上添加多边形
上一篇介绍了在GMap上添加自定义标签(GMapMarker),这篇介绍在GMap上添加多边形(GMapPolyogn),并且介绍如何在地图上画任意的多边形. 如果已经知道了多边形的各个点的位置,就可 ...
- iOS开发之在地图上绘制出你运行的轨迹
首先我们看下如何在地图上绘制曲线.在Map Kit中提供了一个叫MKPolyline的类,我们可以利用它来绘制曲线,先看个简单的例子. 使用下面代码从一个文件中读取出经纬度,然后创建一个路径:MKPo ...
- R语言绘图:在地图上绘制热力图
使用ggplot2在地图上绘制热力图 ######*****绘制热力图代码*****####### interval <- seq(0, 150000, 25000)[-2] #设置价格区间 n ...
- R语言绘图:在地图上绘制散点图
使用ggplot2在地图上绘制散点图 ######*****绘制散点图代码*****####### options(baidumap.key = '**************') #设置密钥 bei ...
随机推荐
- java之SFTP上传下载
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.ut ...
- spring 整合redis集群中使用@autowire无效问题的解决办法
1.视频参考黑马32期宜立方商城第6课 redis对于的代码 我们先变向一个redis客户端的接口文件 package com.test; public interface JedisClient { ...
- Linux安装Redis 6.0.5 ./install_server.sh报错
Linux安装Redis 6.0.5 ./install_server.sh报错 linux 安装Redis6.0.5时 进行到./install_server.sh时报错, This systems ...
- 如何通过Elasticsearch Scroll快速取出数据,构造pandas dataframe — Python多进程实现
首先,python 多线程不能充分利用多核CPU的计算资源(只能共用一个CPU),所以得用多进程.笔者从3.7亿数据的索引,取200多万的数据,从取数据到构造pandas dataframe总共大概用 ...
- org.hibernate.LazyInitializationException异常解决办法
org.hibernate.LazyInitializationException异常failed to lazily initialize a collection...的解决方案使用hiberna ...
- 记一次WIN10 WLAN消失修复
事故现场:在win10自动更新后 在网路和Internet中WLAN消失 无法发现wifi了 设备管理器中wireless驱动上有黄色感叹号 解决办法: 右键有感叹号的wireless驱动,选择属性, ...
- 问题 C: 最短路径
问题 C: 最短路径 在洛谷上刷最短路的题然后被老师拉回去做算法笔记上面的题... 拿到这道题,先确定所有路径唯一,然后是无向边,那么对于边权处理,直接赋值为2的k次方就可以了,然后直接跑最短路. 这 ...
- lodash - slice
稀疏数组和密集数组 稀疏数组 Sparse arrays 一般来说,JavaScript 中的数组都是稀疏数组-它们可以拥有空槽,所谓空槽,指的就是数组的某个位置没有任何值,既不是 undefined ...
- Isset、empty、count、is_null的比较
1.empty判断变量是否为空, 先把变量转为布尔值再返回:对变量(字符串.数组等)赋值为一切为空的值.或者未定义的变量都返回true,即判断为空,比如null,' ',0,array(),false ...
- 【Python】any() 或者 or
前言 在我之前的文章中有any()和all()的对比:any()和all()对比其中介绍了any()函数的基本特性---可迭代对象中有任意一个不为False的时候,返回True,如果可迭代对象为空的话 ...