更新记录
1.0.0(2024-12-16)
1.新增高德地图一键开启导航、定位
平台兼容性
App |
快应用 |
微信小程序 |
支付宝小程序 |
百度小程序 |
字节小程序 |
QQ小程序 |
HBuilderX 4.36,Android:4.4,iOS:不支持,HarmonyNext:不确定 |
× |
× |
× |
× |
× |
× |
钉钉小程序 |
快手小程序 |
飞书小程序 |
京东小程序 |
鸿蒙元服务 |
× |
× |
× |
× |
× |
H5-Safari |
Android Browser |
微信浏览器(Android) |
QQ浏览器(Android) |
Chrome |
IE |
Edge |
Firefox |
PC-Safari |
× |
× |
× |
× |
× |
× |
× |
× |
× |
lotos-amap
开发文档
UTS 语法
UTS API插件
UTS 组件插件
Hello UTS
示例项目使用指北
- 示例项目下载之后,使用npm或pnpm安装依赖(建议使用pnpm)
- 重新获取appId
- 打包自定义基座(android平台),打包证书需自行解决
- 自行申请高德地图key(android平台,key的包名设置为打包自定义基座时的包名),并在App onLunch中设置
注意事项
- 打包自定义基座运行时,manifest.json中的APP模块配置-maps地图配置不能配置高德key,否则打包会冲突导致失败。
- 插件暴露init方法,可以配置高德地图key(获取方式见高德地图开放平台【(https://lbs.amap.com/api/android-sdk/guide/create-project/get-key)】),建议在app onLaunch中设置。
- 使用本插件时,如果定位不成功,去设置里面查看app定位权限是否开启。
- 本插件暂时只支持android平台,ios平台暂不支持。
配置key(App.vue)
<script setup lang="ts">
import { onLaunch, onShow, onHide } from "@dcloudio/uni-app";
import {
init
} from '@/uni_modules/lotos-amap'
onLaunch(() => {
console.log("App Launch");
init("你的高德地图key")
});
onShow(() => {
console.log("App Show");
});
onHide(() => {
console.log("App Hide");
});
</script>
<style></style>
使用教程(index.nvue)
<script setup lang="ts">
import {
getAmapOnceLocation,
startAmapLocation,
stopLocation,
startNaviNoStartAndEnd,
startNaviWithStartAndEnd,
setScreenOrientation,
permissionsRequest,
calculateRouteJustEnd,
startNavi,
init
} from '@/uni_modules/lotos-amap'
import { ref, onMounted } from 'vue'
const title = ref('Hello')
const naviView = ref(null)
const showNavi = ref(false)
const mapStyle = ref({
width: "0",
height: "0"
})
const routesData = ref([])
const currentSelect = ref()
const showRoute = ref(true)
// 定位一次
const getLocation = () => {
uni.showLoading({
title: '获取定位',
mask: true
})
getAmapOnceLocation({}, res => {
let log = `单次定位回调 ${JSON.stringify(res)}`
console.log(log)
uni.hideLoading()
uni.showToast({
title: log,
icon: 'none'
})
})
}
// 持续定位
const startLocation = () => {
startAmapLocation({
interval: 2000 //定位周期,单位毫秒,不传的话默认2000ms
}, (res) => {
// res是回调结果,拿到之后可以自己处理
let log = `持续定位回调 ${JSON.stringify(res)}`
console.log(log)
uni.showToast({
title: log,
icon: 'none'
})
})
}
const stopLocationHandler = () => {
console.log("停止持续定位")
stopLocation()
}
const startNaviFn = () => {
console.log("开始导航")
startNaviNoStartAndEnd()
}
const startNaviHaveStartAndEnd = () => {
// 起点可以不传入,默认当前位置
const options = {
end: {
latitude: 24.73036,
longitude: 118.650379
}
}
startNaviWithStartAndEnd(options)
}
const calcRouteNoStart = () => {
console.log("开始算路")
const end = {
latitude: 24.73036,
longitude: 118.650379,
address: '德辉广场'
};
calculateRouteJustEnd(end, (res) => {
console.log("算路结果", res)
startNavi(end, 14)
})
}
/**
* 组件算路
*/
const componentCalcRoute = () => {
const end = {
latitude: 24.73036,
longitude: 118.650379,
address: '德辉广场'
};
console.log("naviView", naviView.value)
naviView.value.calcRoute(end)
}
const calcRouteSuccess = (res: any) => {
console.log("算路结果", res.detail.data)
const routesArr = []
for(let key in res.detail.data){
routesArr.push({
id: key * 1,
...res.detail.data[key]
})
}
routesData.value = routesArr
currentSelect.value = routesData.value[0].id
}
const componentStartNavi = () => {
naviView.value.startNavi(currentSelect.value)
mapStyle.value = {
width: "700rpx",
height: "1100rpx"
}
showRoute.value = false
}
const quitNavi = () => {
console.log("组件退出导航")
}
const toSmall = () => {
}
</script>
<template>
<view class="content">
<view class="map-container">
<guitar-navi-view ref="naviView" :style="mapStyle" class="map" @quit="quitNavi" @test="calcRouteSuccess"></guitar-navi-view>
</view>
<view class="calc-result" v-if="showRoute">
<text>算路结果</text>
<view
v-for="item in routesData"
:key="item.id"
:class="{'active': currentSelect === item.id}"
@click="currentSelect = item.id"
>
<view>标签:{{ item.labels }}</view>
</view>
</view>
<view class="btn-box">
<button class="btn" @tap="startLocation">持续定位</button>
<button class="btn" @tap="stopLocationHandler">停止持续定位</button>
<button class="btn" @tap="startNaviFn">无起终点导航</button>
<button class="btn" @tap="startNaviHaveStartAndEnd">有起点终点导航</button>
<button class="btn" @tap="calcRouteNoStart">无起点算路</button>
<button class="btn" @tap="componentCalcRoute">组件算路</button>
<button class="btn" @tap="componentStartNavi">组件开始导航</button>
</view>
</view>
</template>
<style>
.content {
flex-direction: column;
padding-top: 1250rpx;
}
.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-bottom: 50rpx;
}
.text-area {
display: flex;
justify-content: center;
}
.title {
font-size: 36rpx;
color: #8f8f94;
}
.map-container{
position: fixed;
top: 25rpx;
left: 25rpx;
border-radius: 10rpx;
box-shadow: 3rpx 5rpx 10rpx rgba(0, 0, 0, 0.2);
}
.map{
animation: 0.4s all;
}
.calc-result {
position: fixed;
top: 0;
left: 0;
}
.btn-box{
margin-top: 20rpx;
flex-direction: row;
flex-wrap: wrap;
}
.btn{
margin: 10rpx 10rpx;
}
.active {
background-color: pink;
}
</style>