uniapp项目中接入高德地图
时间: 2023-09-12 13:10:57 浏览: 147
要在uniapp项目中接入高德地图,可以按照以下步骤进行操作:
1. 在高德开放平台申请应用并获取应用的AppKey。
2. 安装uni-app的高德地图插件,可以通过uni-app官方插件市场或npm进行安装。
3. 在App.vue中引入高德地图插件,并初始化地图。
```javascript
<template>
<view>
<map :longitude="longitude" :latitude="latitude" :scale="scale" :markers="markers" :include-points="includePoints"></map>
</view>
</template>
<script>
import QQMapWX from '@/common/js/qqmap-wx-jssdk.min.js';//引入高德地图插件
export default {
data() {
return {
longitude: 113.324520,
latitude: 23.099994,
scale: 14,
markers: [],
includePoints: []
};
},
onReady() {
this.mapCtx = uni.createMapContext('map');
this.initMap();
},
methods: {
//初始化地图
initMap() {
let key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';//AppKey
let qqmapsdk = new QQMapWX({
key: key
});
qqmapsdk.reverseGeocoder({
success: res => {
let { latitude, longitude } = res.result.location;
this.longitude = longitude;
this.latitude = latitude;
this.markers.push({
id: 0,
longitude: this.longitude,
latitude: this.latitude,
iconPath: "/static/img/map-marker.png",
width: 30,
height: 30
});
this.includePoints.push({
latitude: this.latitude,
longitude: this.longitude
});
}
});
}
}
};
</script>
```
4. 在manifest.json文件中配置高德地图插件的AppKey。
```json
{
"mp-weixin": {
"appid": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"miniprogramRoot": "pages",
"plugins": {
"myPlugin": {
"version": "1.0.0",
"provider": "wxid"
},
"myOtherPlugin": {
"version": "1.2.3",
"provider": "wxid"
},
"myAmap": {
"version": "1.0.0",
"provider": "wxid",
"options": {
"apiKey": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}
}
}
}
```
这样就可以在uniapp项目中成功接入高德地图了。
阅读全文