一、 高德地图开放平台申请key
-
进入控制台
-
我的应用中创建新应用
-
添加key
-
填写信息,提交
-
记住key和安装秘钥,后面项目中会用到
二、vue项目中使用
- 安装loader和amap
npm i @amap/amap-jsapi-loader -S
npm i vue-amap -S
- 使用
import AMapLoader from '@amap/amap-jsapi-loader'
window._AMapSecurityConfig = {
securityJsCode: '输入安全秘钥'
}
export default {
data(){
return{
zoom: 18, // 缩放比例
position:{
longitude: 115.383020,
latitude: 39.955792
},
marker: null,
}
},
components:{Breadcrumb,},
mounted(){
this.initMap()
},
methods:{
initMap(){
AMapLoader.load({
key: '输入key',
version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
plugins: [
'AMap.AutoComplete', 'AMap.PlaceSearch', 'AMap.Scale',
'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType',
'AMap.PolyEditor', 'AMap.CircleEditor','AMap.Driving'
] , // 需要使用的的插件列表,如比例尺'AMap.Scale'等
AMapUI: {
// 是否加载 AMapUI,缺省不加载
version: "1.1", // AMapUI 缺省 1.1
plugins: [], // 需要加载的 AMapUI ui插件
},
}).then(AMap => {
this.map = new AMap.Map('map', {
//设置地图容器id
// viewMode: '3D', //是否为3D地图模式
zoom: this.zoom, //初始化地图级别
center: [this.position.longitude, this.position.latitude] //初始化地图中心点位置
})
this.marker = new AMap.Marker({
map: this.map,
position: [this.position.longitude, this.position.latitude],
})
}).catch(e => {
console.log(e)
})
}
}
}
</script>
- 效果