申请key
1、注册登陆 高德开放平台,申请 key
添加 Key
注意
<script type="text/javascript">
window._AMapSecurityConfig = {
securityJsCode:'您申请的安全密钥',
}
</script>
放在 index.html
文件里面,密钥必须比 key 先加载就行了
导入 vue-amap
安装 vue-amap
npm install vue-amap --save
安装完成后,main.js
文件中引入
import VueAMap from "vue-amap";
VueAMap.initAMapApiLoader({
key: "KEY",
plugin: [
"AMap.Autocomplete", //输入提示插件
"AMap.PlaceSearch", //POI搜索插件
"AMap.Scale", //右下角缩略图插件 比例尺
"AMap.OverView", //地图鹰眼插件
"AMap.ToolBar", //地图工具条
"AMap.MapType", //类别切换控件,实现默认图层与卫星图、实施交通图层之间切换的控制
"AMap.PolyEditor", //编辑 折线多,边形
"AMap.CircleEditor", //圆形编辑器插件
"AMap.Geolocation" //定位控件,用来获取和展示用户主机所在的经纬度位置
],
v: '1.4.4',
uiVersion: '1.0'
});
Vue.use(VueAMap);
使用
<template>
<el-row>
<el-col :span="22" :offset="1" >
<div class="amap-page-container">
<el-amap-search-box class="search-box"
:search-option="amap.searchOption"
:on-search-result="onSearchResult"
:center="amap.center"></el-amap-search-box>
<div style="width: 900px;height: 400px;margin-top: -35px; border: #2c3e50 solid 1px;">
<el-amap vid="amapDemo" :zoom="amap.zoom" :center="amap.center" class="amap-demo">
<el-amap-marker :position="amap.marker.position" :events="amap.marker.events"
:visible="amap.marker.visible" :draggable="amap.marker.draggable"></el-amap-marker>
</el-amap>
</div>
</div>
</el-col>
</el-row>
</template>
<!-- vid = 地图容器节点的ID。
zoom = 缩放级别
center = 地图可见区域的中心点
marker = 覆盖物
visible = 显示点标记
draggable = 设置点标记是否可拖拽移动,默认为false。
-->
<style>
.search-box {
position: absolute;
top: 25px;
left: 25px;
}
</style>
<script>
module.exports = {
name: 'amap-page',
data() {
return {
amap: {
zoom: 14,
center: [116.7833687777778, 31.89548288888889],
marker: {
position: [116.7833687777778, 31.89548288888889],
events: {
click: (e) => {
alert('click marker');
let {lng, lat} = e.lnglat;
this.amap.lng = lng;
this.amap.lat = lat;
this.amap.center = [e.lnglat.lng, e.lnglat.lat]
},
dragend: (e) => {
console.log('---event---: dragend')
console.log(e.lnglat.lng, e.lnglat.lat)
this.amap.marker.position = [e.lnglat.lng, e.lnglat.lat];
}
},
visible: true,
draggable: false, // 设置点标记是否可拖拽移动,默认为false。
},
// lng: 0,
// lat: 0,
// address:'',
// q: '',
searchOption: {
city: '全国',
citylimit: true
}
},
form:{
lng:"",
lat:"",
address:""
}
};
},
methods: {
onSearchResult(pois) {
let latSum = 0
let lngSum = 0
pois.forEach(poi => {
lngSum += poi.lng
latSum += poi.lat
})
let center = {
lng: lngSum / pois.length,
lat: latSum / pois.length
}
var address = pois[0].address;
console.log(center.lng, center.lat);
this.amap.center = [center.lng, center.lat]
this.amap.marker.position = [center.lng, center.lat]
this.form.lng = center.lng;
this.form.lat = center.lat;
this.form.address = address;
console.log(this.form.address);
},
}
};
</script>
效果
公交站点搜索
参考: https://blog.csdn.net/reactL/article/details/84779468
<template>
<el-row>
<el-col :span="22" :offset="1" >
<div class="amap-page-container">
<el-amap-search-box class="search-box"
:search-option="amap.searchOption"
:on-search-result="onSearchResult"
:center="amap.center"></el-amap-search-box>
<div style="width: 900px;height: 400px;margin-top: -35px; border: #2c3e50 solid 1px;">
<el-amap vid="amapDemo" :zoom="amap.zoom" :center="amap.center" class="amap-demo">
<el-amap-marker v-for="(marker, index) in amap.markers" :events="marker.events"
:position="marker.point" :icon="marker.icon" :key="index"/>
</el-amap>
</div>
</div>
</el-col>
</el-row>
</template>
<!-- vid = 地图容器节点的ID。
zoom = 缩放级别
center = 地图可见区域的中心点
marker = 覆盖物
visible = 显示点标记
draggable = 设置点标记是否可拖拽移动,默认为false。
-->
<style>
.search-box {
position: absolute;
top: 25px;
left: 25px;
}
</style>
<script>
module.exports = {
name: 'amap-page',
data() {
return {
amap: {
zoom: 14,
center: [116.7833687777778, 31.89548288888889],
searchOption: {
city: '0551',
citylimit: true
},
stationSearch:{},
markers: [
{
point: [116.7833687777778, 31.89548288888889],
events: {
click: (e) => {
// alert('click marker');
let {lng, lat} = e.lnglat;
this.amap.lng = lng;
this.amap.lat = lat;
this.amap.center = [e.lnglat.lng, e.lnglat.lat]
}
},
}
],
},
form:{
lng:"",
lat:"",
address:""
}
};
},
methods: {
onSearchResult(pois) {
console.log(pois,"pois");
let latSum = 0
let lngSum = 0
pois.forEach(poi => {
lngSum += poi.lng
latSum += poi.lat
})
let center = {
lng: lngSum / pois.length,
lat: latSum / pois.length
}
var address = pois[0].address;
var name = pois[0].name;
// console.log(center.lng, center.lat);
// this.amap.center = [center.lng, center.lat]
// this.amap.marker.position = [center.lng, center.lat]
// this.form.lng = center.lng;
// this.form.lat = center.lat;
// this.form.address = address;
// console.log(this.form.address);
this.getBusStation(name);
},
// 获取站点信息列表,使用父组件传入输入框的值
getBusStation(name) {
console.log('name',name);
this.stationSearch = {
pageIndex: 1, // 页码
pageSize: 30, // 单页显示结果条数
city: '0551' // 确定搜索城市
}
var stationList = new AMap.StationSearch(this.stationSearch)
// 一:
// stationList.search('小', function (status, result) {
// // result即是对应的公交站点数据信息
// if (status === 'complete' && result.info === 'OK') {
// console.log(result)
// console.log(this.stationListData)
// } else {
// console.log('公交路线数据查询失败' + result)
// }
// })
// 二:
stationList.search(name) // mock一个假数据
let _this = this;
new AMap.event.addListener(stationList, 'complete', (res) => {
// res为获取到的当前位置的信息
this.stationListData = res
console.log(this.stationListData,"stationListData")
this.stationListData.stationInfo.forEach(data=>{
console.log("data=>",data)
let id = data.id;
let dlng = data.location.lng;
let dlat = data.location.lat;
_this.amap.markers.push({point: [dlng, dlat],
events: {
click: (e) => {
let {lng, lat} = e.lnglat;
console.log('click marker 坐标:',e.lnglat);
console.log('click marker 公交id:',id);
this.amap.lng = lng;
this.amap.lat = lat;
this.amap.center = [lng, lat]
this.$confirm('是否确认提交站点数据?', "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
}).then(function() {
}).then(() => {
this.msgSuccess("提交成功");
}).catch(function() {});
}
}
});
_this.amap.center = [dlng, dlat]
})
}) // 返回定位信息
new AMap.event.addListener(stationList, 'error', (err) => {
console.log(err)
}) // 返回定位出错信息
},
}
};
</script>