一、如何在Vue中引入基础高德地图
根据官网提示,在Web端,我们需要高德地图加载器:npm i @amap/amap-jsapi-loader -S
注意: * 此教程适用于Web端,移动端还未尝试,感兴趣的或者有需要的可单独联系我 * @amap/amap-jsapi-loader加载器npm地址
1、步骤一:注册并登录高德地图开放平台,申请密钥
高德地图开放平台 操作步骤如下图:
2、步骤二:安装高德地图加载器
npm i @amap/amap-jsapi-loader -S
3、封装一个自定义地图组件,并初始化地图
<template>
<!-- 百度地图 -->
<div id="bai-du-map">
<!-- 技术支持和联系方式 -->
</div>
</template>
<script>
import AMapLoader from "@amap/amap-jsapi-loader";
window._AMapSecurityConfig = {
securityJsCode: '安全密钥',
}
export default {
data() {
return {
map : null,
mouseTool : null,
overlays : [],
auto : null,
placeSearch : null,
}
},
methods : {
initMap() {
AMapLoader.load({
"key": "申请好的Web端开发者Key,首次调用 load 时必填",
"version": "2.0",
"plugins": [地图用到的插件],
}).then((AMap)=>{
this.map = new AMap.Map('bai-du-map',{
viewMode : "2D",
zoom : 13,
center : [113.65553,34.748764],
resizeEnable: true
});
}).catch(e => {
console.log(e);
});
}
};
</script>
<style scoped>
#bai-du-map {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0;
font-family: "微软雅黑";
}
</style>
<style>
.amap-logo {
display: none !important;
}
.amap-copyright {
display: none !important;
}
</style>
注意点:
2021年起,高德地图必须使用安全密钥 即是
window._AMapSecurityConfig = {
securityJsCode: '安全密钥',
}
二、根据关键词搜索,并定位到搜索的位置
<template>
<!-- 百度地图 -->
<div id="bai-du-map">
<!-- 技术支持和联系方式 -->
</div>
</template>
<script>
import AMapLoader from "@amap/amap-jsapi-loader";
window._AMapSecurityConfig = {
securityJsCode: '安全密钥',
}
export default {
props : ["iptId"],
data() {
return {
map : null,
mouseTool : null,
overlays : [],
auto : null,
placeSearch : null,
}
},
methods : {
initMap() {
AMapLoader.load({
"key": "密钥",
"version": "2.0",
"plugins": ["AMap.AutoComplete","AMap.PlaceSearch"],
}).then((AMap)=>{
this.map = new AMap.Map('bai-du-map',{
viewMode : "2D",
zoom : 13,
center : [113.65553,34.748764],
resizeEnable: true
});
this.auto = new AMap.AutoComplete({
input : this.iptId
});
this.placeSearch = new AMap.PlaceSearch({
map: this.map,
panel: "panel",
autoFitView: true,
extensions: 'base'
});
this.auto.on("select", this.select);
}).catch(e => {
console.log(e);
});
},
select(e) {
this.placeSearch.setCity(e.poi.adcode);
this.placeSearch.search(e.poi.name);
}
},
mounted() {
this.initMap();
},
};
</script>
三、添加放大缩小地图、转盘
<template>
<!-- 百度地图 -->
<div id="bai-du-map">
<!-- 技术支持和联系方式 -->
</div>
</template>
<script>
import AMapLoader from "@amap/amap-jsapi-loader";
window._AMapSecurityConfig = {
securityJsCode: '安全密钥',
}
export default {
props : ["iptId"],
data() {
return {
map : null,
mouseTool : null,
overlays : [],
auto : null,
placeSearch : null,
}
},
methods : {
initMap() {
AMapLoader.load({
"key": "密钥",
"version": "2.0",
"plugins": ["AMap.Scale","AMap.ToolBar","AMap.ControlBar","AMap.MouseTool","AMap.MapType","AMap.HawkEye"],
}).then((AMap)=>{
this.map = new AMap.Map('bai-du-map',{
viewMode : "2D",
zoom : 13,
center : [113.65553,34.748764],
resizeEnable: true
});
this.map.addControl(new AMap.Scale());
let toolBar = new AMap.ToolBar({
position: {
bottom: '210px',
right: '35px'
}
});
let controlBar = new AMap.ControlBar({
position: {
bottom: '280px',
right: '10px',
},
});
this.map.addControl(toolBar);
this.map.addControl(controlBar);
this.map.addControl(new AMap.MapType());
this.map.addControl(new AMap.HawkEye());
}).catch(e => {
console.log(e);
});
},
},
mounted() {
this.initMap();
},
};
</script>
四、点击地图获取经纬度
initMap() {
AMapLoader.load({
"key": "key",
"version": "2.0",
"plugins": ["AMap.Scale","AMap.ToolBar","AMap.AutoComplete","AMap.PlaceSearch","AMap.ControlBar","AMap.MouseTool"],
}).then((AMap)=>{
this.map.on('click', function(e) {
console.log("经度",e.lnglat.getLng())
console.log("纬度",e.lnglat.getLat())
});
}).catch(e => {
console.log(e);
});
},
五、地图上绘制标记点、圆形、曲线、矩形和多边形等,及其绘制清除
<template>
<!-- 百度地图 -->
<div id="bai-du-map">
<!-- 技术支持和联系方式 -->
</div>
</template>
<script>
import AMapLoader from "@amap/amap-jsapi-loader";
window._AMapSecurityConfig = {
securityJsCode: '安全密钥',
}
export default {
data() {
return {
map : null,
mouseTool : null,
overlays : [],
auto : null,
placeSearch : null,
}
},
methods : {
initMap() {
AMapLoader.load({
"key": "密钥",
"version": "2.0",
"plugins": ["AMap.Scale","AMap.ToolBar","AMap.AutoComplete","AMap.PlaceSearch","AMap.ControlBar","AMap.MouseTool"],
}).then((AMap)=>{
this.map = new AMap.Map('bai-du-map',{
viewMode : "2D",
zoom : 13,
center : [113.65553,34.748764],
resizeEnable: true
});
this.mouseTool = new AMap.MouseTool(this.map);
this.mouseTool.on('draw',function(e){
this.overlays.push(e.obj);
}.bind(this))
}).catch(e => {
console.log(e);
});
},
draw(type) {
switch(type){
case 'marker':{
this.mouseTool.marker({
});
break;
}
case 'polyline':{
this.mouseTool.polyline({
strokeColor:'#80d8ff'
});
break;
}
case 'polygon':{
this.mouseTool.polygon({
fillColor:'#00b0ff',
strokeColor:'#80d8ff'
});
break;
}
case 'rectangle':{
this.mouseTool.rectangle({
fillColor:'#00b0ff',
strokeColor:'#80d8ff'
});
break;
}
case 'circle':{
this.mouseTool.circle({
fillColor:'#00b0ff',
strokeColor:'#80d8ff'
});
break;
}
}
},
},
mounted() {
this.initMap();
},
};
</script>
清除绘制
六、总结
一般的,使用高德地图,不需要参考其他博主写的,基本上就是安装一个加载器@amap/amap-jsapi-loader,然后参考高德地图开放webjs文档,就完全可以自己实现业务需求
高德地图开放webjs文档 ,这个API文档写的很好,包括了针对于Vue和React的基本引用,以及相关地图需求都有API满足
|