【vue2 中使用高徳地图amap】项目中需要加载地图, 选择使用高徳地图(更易懂易用稳定些)
1.首先需要在高徳地图官网申请一个开发者账号 得到一个key
2.在vue2 的index.html 中加载高徳js库
//AMap // AMapUI
3.在webpack.base.config.js 中外部库选项中引入
externals: {
"AMap":"window.AMap",
"AMapUI":"window.AMapUI",
},
4.结果是这样的
文章图片
5.开始你的地图代码了
在mounted() 中初始化加载initMap
async initMap(){
this.map = await new AMap.Map('map_container', {
resizeEnable: true
})this.map.plugin(["AMap.ToolBar"],()=> {
//设置地位标记为自定义标记
let toolBar = new AMap.ToolBar();
this.map.addControl(toolBar);
});
//这是map上增加的图层
AMapUI.loadUI(['misc/PoiPicker'], (PoiPicker)=>{
this.poiPicker = new PoiPicker({
input: 'keyword', //搜索关键词id
placeSearchOptions: {
map: this.map,
pageSize: 5
},
searchResultsContainer: 'searchResults'//搜索结果区
})//地图上增加信息窗体
this.infoWindow = new AMap.InfoWindow({
isCustom:true,
offset: new AMap.Pixel(0, -20),
content: this.$refs.lbsInfoComponent.$el, //窗体内容,使用vue 组件
})//选中搜索结果事件
this.poiPicker.on('poiPicked', (poiResult) =>{this.poiPicker.hideSearchResults()
let source = poiResult.source
let poi = poiResult.item
let info = {
source: source,
id: poi.id,
name: poi.name,
location: poi.location.toString(),
address: poi.address
}
this.infoWindow.setMap(this.map)
this.infoWindow.setPosition(poi.location)this.$refs.lbsInfoComponent.initialize(info, this.lbsDistance)
if (source !== 'search') {
this.poiPicker.searchByKeyword(poi.name)
} else {
}
})})//行政区划查询
let opts = {
subdistrict: 1,//返回下一级行政区
showbiz:false//最后一级返回街道信息
}
this.district = new AMap.DistrictSearch(opts)
this.district.search('中国', (status, result)=>{
if(status=='complete'){this.getData(result.districtList[0])//加载省份
}
})
},getData(data) {
let bounds = data.boundaries
if (bounds) {
for (let bound of bounds) {
let polygon = new AMap.Polygon({
map: this.map,
strokeWeight: 1,
strokeColor: '#CC66CC',
fillColor: '#CCF3FF',
fillOpacity: 0.5,
path: bound
})
this.polygons.push(polygon)
}
this.map.setFitView()//地图自适应
}let level = ""let curList = []
if(data.districtList){
for(let item of data.districtList){
level = item.level
curList.push({name: item.name, center:item.center, adcode: item.adcode, cityCode: item.cityCode, level:item.level})
}
}if(level === "province"){
this.provinces = curList
}
else if(level === "city"){
this.cities = curList
}
else if(level === "district"){
this.districts = curList
}
else if(level === "street"){
this.streets = curList
}},
推荐阅读
- 【el-table】固定第一行,其他行可以鼠标滑动滚动,固定列用fixed属性
- vue.js|基于Springboot+Vue+小程序学生课程考勤系统设计
- Vue.js|vue.js中常用v-指令
- vue.js|vue中常见的v指令
- dataurl|第七天 黑马十次方 吐槽列表与详细页、发吐槽与评论功能、问答频道功能、掌握DataURL和阿里云OSS
- IntelliJ|Vue学习笔记(狂神)
- vue.js|狂神说 Vue 笔记
- uniapp|uniapp显示日期格式化
- vue.js|vue数据push后不能响应式更新