最全vue的vue-amap使用高德地图插件画多边形范围
一、在vue-cli的框架下的main.js(或者main.ts)中引入高德插件,代码如下:
import Vue from 'vue'
import VueAMap from 'vue-amap'
import ElementUI from 'element-ui' import App from './App.vue'
import router from './router'
import store from './store'
import './registerServiceWorker' Vue.use(VueAMap)
Vue.use(ElementUI) VueAMap.initAMapApiLoader({
// 高德的key
key: '你的高德key',
// 插件集合
plugin: [
'AMap.Autocomplete',
'AMap.PlaceSearch',
'AMap.Scale',
'AMap.OverView',
'AMap.ToolBar',
'AMap.MapType',
'AMap.PolyEditor',
'AMap.CircleEditor',
'AMap.Geocoder',
'AMap.Geolocation'
],
// 高德 sdk 版本,默认为 1.4.4
v: '1.4.10'
}) Vue.config.productionTip = false new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
第三种画多边形的效果图:
注意:1、这种画多边形,开始就需要一个初始的多边形;
2、所以,输入要画多边形范围的地名,点击搜索,地图会跳转到搜索的地方,同时得到经纬度;
3、点“范围绘制”时,我再方法里根据第2步的经纬度,初始了一个多边形;
****隐藏彩蛋****
下图的 “请输入经纬度” 可以输入一大组的经纬度,按回车键,也可以画出多边形,在按“范围绘制”也可以更改;(格式如下:)
这个格式就是复制的地图上显示的经纬度坐标
106.2246 , 29.59258
106.225064 , 29.593287
106.226137 , 29.593558
106.22692 , 29.593083
二、第一种画化:使用Geolocation画多边形(效果是在地图点了,才会形成多边形)
// 新增 编辑 查看
<template>
<div class="point">
<el-header></el-header>
<div class="action-bar">
<el-form class="inline-form" :rules="rules" ref="formData" size="small" :model="formData">
<el-form-item label label-width="220" prop="location">
<el-input
:disabled="!ifFalg"
class="name-input"
clearable
v-model="formData.location"
placeholder="名称"
maxlength="30"
></el-input>
</el-form-item>
<el-form-item label prop="longitude">
<el-input
:disabled="!ifFalg"
class="my-input"
clearable
v-model.number="formData.longitude"
placeholder="经度 "
></el-input>
</el-form-item>
<el-form-item label prop="latitude">
<el-input
:disabled="!ifFalg"
class="my-input"
clearable
v-model.number="formData.latitude"
placeholder="纬度"
></el-input>
</el-form-item>
<el-button class="my-button" v-if="ifFalg" type="primary" @click="save" size="small">保存</el-button>
<el-button class="my-button" size="small" @click="close">关闭</el-button>
</el-form>
</div>
<div class="map-box">
<div class="map-tool">
<div v-if="ifFalg">
<el-checkbox v-model="enterType">地图上描点</el-checkbox>
</div>
<!-- <el-checkbox @change="checkbox" v-model="enterType">地图上描点</el-checkbox> -->
<div class="longlat">
<ul>
<li v-for="(item, index) in lnglatpoints" :key="index">
{{item.longitude}} , {{item.latitude}}
<i
v-if="ifFalg"
class="el-icon-close"
@click="deletes(item)"
></i>
</li>
</ul>
<el-input
v-if="ifFalg"
class="my-input"
size="small"
clearable
v-model="lngLat"
@keyup.enter.native="submitEnter"
placeholder="请输入经纬度"
></el-input>
<el-button v-if="ifFalg" size="small" @click="clear" type="primary" class="claer">清除</el-button>
</div>
</div>
<div class="map" id="map">
<el-amap
ref="map"
bubble
:plugin="plugin"
:zoom="map.zoom"
:center="map.center"
:events="events"
id="amap"
>
<el-amap-polygon
:events="plugin.events"
:path="path"
:draggable="draggable"
fillColor="#2b83f9"
fillOpacity="0.5"
strokeWeight="0"
strokeColor="#2b83f9"
strokeOpacity="0.5"
></el-amap-polygon>
<!-- <el-amap-marker :position="marker.position" :events="plugin.events"></el-amap-marker> -->
<el-amap-marker v-if="formData.type === 1" :position="map.center" :label="label"></el-amap-marker>
</el-amap>
</div> </div>
</div>
</template>
<script lang="ts">
import * as api from '@/utils/api/index'
import { Component, Vue } from 'vue-property-decorator'
import eHeader from '@/components/header.vue'
import { constants } from 'http2'
import * as util from '@/utils/util.ts' const testLongitude = (rule: any, value: string, callback: Function) => {
if (util.regExp.longitudeRegExp.test(value)) {
return callback()
} else {
return callback(new Error('请输入正确的经度'))
}
}
const testLatitude = (rule: any, value: string, callback: Function) => {
if (util.regExp.latitudeRegExp.test(value)) {
return callback()
} else {
return callback(new Error('请输入正确的纬度'))
}
}
@Component({
components: {
'el-header': eHeader
}
})
export default class point extends Vue {
private breadcrumbId = 0
private id = ''
private lngLat = ''
private ifFalg = true
private map = {
zoom: 15,
center: [106.55073, 29.56471]
}
private path: any = []
private draggable = false
private lnglatpoints: any = []
private enterType = false // 录入坐标 | 地图上描点
private cities = []
private formData = {
location: '',
longitude: '',
latitude: ''
}
plugin = {
pName: 'Geolocation',
events: {}
}
events = {}
private test = 1 private rules = {
location: [
{ required: true, message: '请输入接送点名称', trigger: 'blur' }
],
longitude: [{ validator: testLongitude, trigger: 'blur' }],
latitude: [{ validator: testLatitude, trigger: 'blur' }]
} mounted() {
this.id = this.$route.params.id
this.breadcrumbId = Number(this.$route.query.breadcrumbId)
if (this.breadcrumbId === 2) {
this.ifFalg = false
}
if (this.id !== '-1') {
this.details()
} // this.city()
let _this: any = this // 地图点击事件
_this.events = {
click: (e: any) => {
if (this.enterType) {
this.path = []
console.log(e.lnglat)
let lnglat = e.lnglat
this.lnglatpoints.push({
latitude: lnglat.lat,
longitude: lnglat.lng
})
console.log(this.lnglatpoints)
this.lnglatpoints.map((val: any, index: number) => {
console.log(index)
if (index === 0) {
this.map.center = [val.longitude, val.latitude]
}
let arr = [val.longitude, val.latitude]
this.path.push(arr)
})
// this.setFitView()
}
}
} // 多边形点击事件
_this.plugin.events = {
click: (e: any) => {
if (this.enterType) {
this.path = []
console.log(e.lnglat)
let lnglat = e.lnglat
this.lnglatpoints.push({
latitude: lnglat.lat,
longitude: lnglat.lng
})
console.log(this.lnglatpoints)
this.lnglatpoints.map((val: any, index: number) => {
console.log(index)
if (index === 0) {
this.map.center = [val.longitude, val.latitude]
}
let arr = [val.longitude, val.latitude]
this.path.push(arr)
})
// this.setFitView()
}
}
}
}// 获取接送范围集合
details() {
const loading = this.$loading({
lock: true,
text: '加载中...'
})
api.main.boss_line_point__get({ params: {param: this.id}}).then((res: any) => {
if (res.data.success) {
const response = res.data.data
this.formData = response
let points = res.data.data.points
if (points != null) {
for (let i = 0; i < points.length; i++) {
points[i].id = i
}
this.lnglatpoints = points
this.lnglatpoints.map((val: any, index: number) => {
if (index === 0) {
this.map.center = [val.longitude, val.latitude]
}
let arr = [val.longitude, val.latitude]
this.path.push(arr)
})
} else {
this.map.center = [
Number(this.formData.longitude),
Number(this.formData.latitude)
]
this.label.content = this.formData.location
}
setTimeout(this.setFitView, 0)
} else {
this.$message.error(res.data.message)
}
loading.close()
})
} // 移除经纬度
deletes(data: any) {
let e: any = this
this.path = []
for (let i = 0; i < e.lnglatpoints.length; i++) {
if (
data.latitude === e.lnglatpoints[i].latitude &&
data.longitude === e.lnglatpoints[i].longitude
) {
e.lnglatpoints.splice(i, 1)
}
}
console.log(e.path)
this.lnglatpoints.map((val: any, index: number) => {
let arr = [val.longitude, val.latitude]
this.path.push(arr)
if (index === 0) {
this.map.center = [val.longitude, val.latitude]
}
console.log(this.path)
})
} clear() {
this.$confirm('确认删除绘制的接送区域?', '删除', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
})
.then(() => {
let self: any = this
this.path = []
this.lnglatpoints = []
// this.map.center = [106.5507300000, 29.5647100000]
this.lngLat = ''
self.formData.points = []
})
.catch(() => {})
} // 输入经纬度
submitEnter() {
// eslint-disable-next-line
const illegalRegExp = /^(\D|\d*\.?\d*,*\s)|[^\d\s,\.]|^\d*\.?\d*$|(,\.|\.,)+|(\d*\.*\d*,){2,}|(\d*\.){2,}|(\d*\s){2,}|(\s\d*\.?\d*|\D)$/g
const replaceWhiteSpaceRegExp = /(?<=(,|\.|\s))\s+|\s+(?=(,|\.))|^\s|\s+$/g this.lngLat = this.lngLat.replace(replaceWhiteSpaceRegExp, '')
if (illegalRegExp.test(this.lngLat)) {
return this.$message.error('经纬度格式错误!')
}
const lnglatArray = this.lngLat.split(' ')
lnglatArray.forEach(lnglatString => {
const lnglatObject = {
longitude: lnglatString.split(',')[0],
latitude: lnglatString.split(',')[1]
}
this.lnglatpoints.push(lnglatObject)
})
this.path = []
this.lnglatpoints.map((val: any, index: number) => {
let arr = [val.longitude, val.latitude]
this.path.push(arr)
this.lngLat = ''
if (index === 0) {
this.map.center = [val.longitude, val.latitude]
}
})
} setFitView() {
const vm: any = this
let map = vm.$refs.map.$$getInstance()
map.setFitView()
} close() {
this.$router.push({
name: 'pointList'
})
} save() {
let e: any = this
let params: any = {}
if (this.id !== '-1') {
// 编辑
e.formData.id = this.id
params.id = this.id
}
e.formData.points = this.lnglatpoints
if (e.formData.location === '' || e.formData.location === null) {
this.$message.warning('名称不能为空!')
return
}
if (this.lnglatpoints.length < 3 && e.formData.type === 2) {
this.$message.warning('经纬度不能小于三组!')
return
}
params.points = this.lnglatpoints
params.location = this.formData.location
params.longitude = this.formData.longitude
params.latitude = this.formData.latitude
if (this.id !== '-1') {
api.main.boss_line_point_update_post({ data: params }).then((res: any) => {
if (res.data.success) {
this.$message.success('保存成功!')
this.$router.push({
name: 'pointList'
})
} else {
this.$message.error(res.data.message)
}
})
} else {
api.main
.boss_line_point_addAndBindLine_post({ data: params })
.then((res: any) => {
if (res.data.success) {
this.$message.success('保存成功!')
this.$router.push({
name: 'pointList'
})
} else {
this.$message.error(res.data.message)
}
})
}
}
}
</script>
<style lang="scss" scoped>
ul,
li {
list-style: none;
margin: 0;
padding: 0;
}
.inline-form {
display: flex;
display: -webkit-flex;
flex-direction: row;
flex-wrap: wrap;
.el-form-item {
margin-bottom: 10px;
margin-left: 15px;
display: flex;
}
.el-button {
margin-left: 15px;
height: 32px;
}
}
.action-bar {
box-sizing: border-box;
padding: 10px;
padding-bottom: 0; border: {
top: 1px solid #ddd;
bottom: 1px solid #ddd;
}
.my-input {
width: 150px;
}
.name-input {
width: 260px;
}
}
.el-select-dropdown__item {
background-color: white;
text-indent: 10px;
}
.claer {
margin-top: 15px;
float: right;
} $map_height: calc(100vh - 55px - 50px - 75px - 15px);
.map-box {
position: relative; height: $map_height;
.map-tool {
position: absolute;
width: 220px;
z-index: 170;
top: 0;
left: 0;
max-height: 100%; box-sizing: border-box;
padding: 10px;
overflow-y: auto; background-color: #fff;
box-shadow: 2px 4px 7px 1px #dedede;
}
.map {
transition: all 0.6s;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
}
.swiper-box {
position: relative;
z-index: 161; display: flex;
align-items: center;
flex-direction: row;
justify-content: center; width: 100%; transition: transform ease-in 0.6s;
transform: translateX(0);
white-space: nowrap;
.swiper-item {
width: 100%;
height: $map_height;
}
}
.hide-text-area {
transform: translateX(-100%);
}
.gray-map {
filter: grayscale(90%);
}
.longlat {
margin-top: 15px;
padding-bottom: 15px; ul {
li {
padding: 6px;
background-color: #ddd;
border-radius: 4px;
margin-bottom: 15px;
font-size: 14px;
color: #666;
position: relative;
}
}
}
.el-icon-close {
display: inline-block;
position: absolute;
right: 10px;
color: #000 !important;
cursor: pointer;
}
.my-button {
margin-bottom: 10px;
}
</style>
三、第二种画化:使用AMap.MouseTool画多边形(效果是:多边形随鼠标左键点击,多边形直接跟着变化)
// 新增 编辑 查看
<template>
<div class="point">
<el-header></el-header>
<div class="action-bar">
<el-form class="inline-form" :rules="rules" ref="formData" size="small" :model="formData">
<el-form-item label prop="location">
<el-input
:disabled="!ifFalg"
class="name-input"
clearable
v-model="formData.location"
placeholder="名称"
maxlength="30"
></el-input>
</el-form-item>
<el-form-item label prop="longitude">
<el-input
:disabled="!ifFalg"
class="my-input"
clearable
v-model.number="formData.longitude"
placeholder="经度 "
></el-input>
</el-form-item>
<el-form-item label prop="latitude">
<el-input
:disabled="!ifFalg"
class="my-input"
clearable
v-model.number="formData.latitude"
placeholder="纬度"
></el-input>
</el-form-item>
<el-button class="my-button" v-if="ifFalg" type="primary" @click="save" size="small">保存</el-button>
<el-button class="my-button" size="small" @click="close">关闭</el-button>
</el-form>
</div>
<div class="map-box">
<div class="map-tool">
<div v-if="ifFalg">
<el-checkbox >地图上描点</el-checkbox>
</div>
<div class="longlat">
<ul><li v-for="(item, index) in lnglatpoints" :key="index">
{{item.longitude}} , {{item.latitude}}
<i
v-if="ifFalg"
class="el-icon-close"
@click="deletes(item)"
></i>
</li>
</ul>
<br>
<div>
<span >输入范围经纬度:</span>
<el-input
type="textarea"
autosize
placeholder="请输入内容"
v-model="lnglatpointsString">
</el-input>
</div>
<el-button v-if="ifFalg" size="small" @click="clear1" type="primary" class="claer1">确定</el-button>
<el-button v-if="ifFalg" size="small" @click="clear" type="primary" class="claer">清除</el-button>
</div>
</div>
<div class="map" id="map">
<el-amap
ref="map"
bubble
:zoom="map.zoom"
:center="map.center"
:events="mapEvents"
id="amap"
>
<el-amap-polygon
:events="plugin.events"
:path="path"
fillColor="#2b83f9"
fillOpacity="0.5"
strokeWeight="0"
strokeColor="#2b83f9"
strokeOpacity="0.5"
></el-amap-polygon>
<el-amap-marker v-if="formData.type === 1" :position="map.center" :label="label"></el-amap-marker>
</el-amap> </div>
<div class="my-tools">
<el-row>
<el-button type="primary" v-if="ifFalg" @click="drawPolygon()">鼠标绘制</el-button>
<el-button type="primary" v-if="ifFalg" @click="polygonEditor.close()">结束编辑</el-button>
</el-row>
</div>
</div>
</div>
</template>
<script lang="ts">同上
/**
* 绘制多边形
*/
private drawPolygon () {
let vm: any = this
let map = vm.$refs.map.$$getInstance()
map.plugin(['AMap.MouseTool'], function () {
var mouseTool = new AMap.MouseTool(map)
var drawPolygon = mouseTool.polygon()
AMap.event.addListener(mouseTool, 'draw', function (e: any) {
e.obj.Je.visible = false
let path = e.obj.getPath()
vm.drawPolygonsToMap(path) path.forEach((point:any) => {
vm.lnglatpoints.push({
latitude: point.lat,
longitude: point.lng
})
});
// vm.mapDates =path
// e.obj.hide()
mouseTool.close()
})
}) }
同上
}
</script>
<style lang="scss" scoped>
和上面一样
</style>
三、第三种画化:使用AMap.Polygon和AMap.PolyEditor画多边形(推荐,效果是:https://lbs.amap.com/api/javascript-api/example/overlayers/polygon-draw-and-edit)
注意哦:1、以为这种画多边形,先需要3个点来确定初始的多边形,所以添加了一个功能:搜索 (功能:点击搜索名称的经纬度;);
2、然后我再 ‘范围绘制’ 的方法里根据“搜索”得来的经纬度,手动的弄了3个经纬度数组。
3、然后就可以快乐的画图了。(这画图是真的方便,特别是画范围很复杂的)
// 新增 编辑 查看
<template>
<div class="point">
<el-header></el-header>
<div class="action-bar">
<el-form class="inline-form" :rules="rules" ref="formData" size="small" :model="formData">
<el-form-item label prop="location">
<el-input
:disabled="!ifFalg"
class="name-input"
clearable
v-model="formData.location"
placeholder="名称"
maxlength="30"
></el-input>
</el-form-item>
<el-button class="my-button" type="info" @click="getLocation" size="small">搜索</el-button>
<el-form-item label prop="longitude">
<el-input
:disabled="!ifFalg"
class="my-input"
clearable
v-model.number="formData.longitude"
placeholder="经度 "
></el-input>
</el-form-item>
<el-form-item label prop="latitude">
<el-input
:disabled="!ifFalg"
class="my-input"
clearable
v-model.number="formData.latitude"
placeholder="纬度"
></el-input>
</el-form-item>
<el-button class="my-button" v-if="ifFalg" type="primary" @click="save" size="small">保存</el-button>
<el-button class="my-button" size="small" @click="close">关闭</el-button>
</el-form>
</div>
<div class="map-box">
<div class="map-tool">
<div v-if="ifFalg">
<el-checkbox >地图上描点</el-checkbox>
</div>
<div class="longlat">
<ul>
<li v-for="(item, index) in lnglatpoints" :key="index">
{{item.longitude}} , {{item.latitude}}
<i
v-if="ifFalg"
class="el-icon-close"
@click="deletes(item)"
></i>
</li>
</ul>
<br>
<div>
<span >输入范围经纬度:</span>
<el-input
type="textarea"
autosize
placeholder="请输入内容"
v-model="lnglatpointsString">
</el-input>
</div>
<el-button v-if="ifFalg" size="small" @click="clear1" type="primary" class="claer1">确定</el-button>
<el-button v-if="ifFalg" size="small" @click="clear" type="primary" class="claer">清除</el-button>
</div>
</div>
同上
<div class="my-tools">
<el-row>
<el-button type="primary" v-if="ifFalg" @click="drawPolygon()">鼠标绘制</el-button>
<el-button type="primary" v-if="ifFalg" @click="polygonEditor.close()">结束编辑</el-button>
</el-row>
</div>
</div>
</div>
</template>
<script lang="ts">
同上
同上
}
</script>
<style lang="scss" scoped>
和上面一样
</style>
123
最全vue的vue-amap使用高德地图插件画多边形范围的更多相关文章
- vue 里面异步加载高德地图
前言 关于Vue 里面使用异步加载高德地图 项目中其实只有几处需要用到地图,不需要全局引入 在index文件中引入js会明显拖慢首屏加载速度,虽然可以使用异步加载script的方式解决,但是始终觉得不 ...
- vue中使用vue-amap(高德地图)
因为项目要求调用高德地图,就按照官方文档按部就班的捣鼓,这一路上出了不少问题. 前言: vue-cli,node环境什么的自己安装设置推荐一个博客:https://blog.csdn.net/wula ...
- vue+Element-ui 的 el-cascader 做高德地图的省市区三级联动并且是异步加载,点击省市区跳转到对应的区(地图可以通过后端返回的点进行标点)
// 完整版高德地图,可以复制代码直接使用 index.html <script type="text/javascript" src="https://webap ...
- react项目中引用amap(高德地图)坑
最近在写一个react项目,用到了需要定位的需求,于是乎自己决定用高德地图(AMap),但是react官方文档的案列很少,大多都是原生JS的方法. 在调用amap的 Geocoder Api 时,一直 ...
- 67-Flutter中高德地图插件的使用
1.注册和建立高德API应用 高德网站:https://lbs.amap.com/ 控制台-应用管理-创建应用 在创建 Key 2.获得SHA1 进入Flutter项目中的android文件夹内,打开 ...
- Vue组件篇——Vue3.0中使用高德地图
VUE-CLI 3.0 中配置高德地图 在项目开发中,地图组件 1.首先,需要注册高德开放平台的账号,并在[应用管理]页面[创建新应用],为应用添加Key值 高德开放平台:https://lbs.am ...
- Vue-Cli 3.0 中配置高德地图
vue 中使用高德地图有两种方式 一.vue-amap 组件 官网: https://elemefe.github.io/vue-amap/#/ 开始的时候是打算用这个组件做地图功能的,但是尝试之后存 ...
- 高德地图搜索功能以及清除搜索结果maker
第一次写文章,写得不好各位看官见谅~ (pσ_σ)P首先这是一个vue里面的项目,高德地图api是直接CDN进来的,所以使用了global来调用,默认已经初始化了一个地图,为了实现一个输入框搜索功能和 ...
- 杂项-Map:高德地图
ylbtech-杂项-Map:高德地图 1.返回顶部 1. 开放分类:地图手机软件高德地图(Amap) 是国内一流的免费地图导航产品,也是基于位置的生活服务功能最全面.信息最丰富的手机地图,由国内最大 ...
随机推荐
- Vulkan相关资源
https://github.com/KhronosGroup/Khronosdotorg/blob/master/api/vulkan/resources.md Intel API without ...
- CentOS yum 安装时错误 Errno 14 Couldn't resolve host 解决办法
在虚拟机上安装完CentOS6.5之后,首次使用时yum命令安装软件时,出现一堆的” Errno 14 Couldn't resolve host”这个问题. 上网上查了半天,很多都说在/etc/re ...
- [BUUOJ记录] [CISCN 2019 初赛]Love Math & [NESTCTF 2019]Love Math 2
主要考察利用已有函数构造危险函数绕过,实现RCE. 进入题目给出源码: <?php error_reporting(0); //听说你很喜欢数学,不知道你是否爱它胜过爱flag if(!isse ...
- OpenvSwitch系列之七 meter表限速
Open vSwitch系列之一 Open vSwitch诞生 Open vSwitch系列之二 安装指定版本ovs Open vSwitch系列之三 ovs-vsctl命令使用 Open vSwit ...
- CentOS 7常用命令
常用命令 文件与目录操作 命令 解析 cd /home 进入 ‘/home’ 目录 cd .. 返回上一级目录 cd ../.. 返回上两级目录 cd - 返回上次所在目录 cp file1 file ...
- Intel-Pin的windows安装
环境安装 操作系统:windows10 需要环境: 1.Visual Studio Community 2019 Edition ( https://visualstudio.microsoft.c ...
- 用了这个jupyter插件,我已经半个月没打开过excel了
1 简介 jupyter lab是我迄今为止体验过开展数据分析等任务最舒适的平台,但这不代表它是完美的,因为在很多方面它仍然存在欠缺,譬如在对csv文件的交互式编辑方面. 图1 而本文将要介绍的jup ...
- 2018.12.30【NOIP提高组】模拟赛C组总结
2018.12.30[NOIP提高组]模拟赛C组总结 今天成功回归开始做比赛 感觉十分良(zhōng)好(chà). 统计数字(count.pas/c/cpp) 字符串的展开(expand.pas/c ...
- HTML页面的基本信息
1.python中生成的html页面,每一段的基本解释,以及header中的应用 2.body中的应用 2.1.a href链接点击baidu直接跳转百度网址,如果需要重新打开一个页面,详情看2.16 ...
- [SqlServer]数据库备份-问题及解决
正常数据库备份 备份:右键要备份的数据库-任务-备份 还原:右键数据库-还原数据库 问题1-"还原数据库备份时报错"介质集有2个介质簇,但只提供了1个.必须提供所有成员" ...