mirror of
https://github.com/ialley-workshop-open/uni-halo.git
synced 2026-09-12 16:40:40 +08:00
refactor: 架构升级
This commit is contained in:
@@ -0,0 +1,299 @@
|
||||
# uni.chooseLocation - 选择位置示例
|
||||
|
||||
## 官方文档
|
||||
|
||||
参考官方文档:https://doc.dcloud.net.cn/uni-app-x/api/location/choose-location.html#chooselocation
|
||||
|
||||
## 概述
|
||||
|
||||
`uni.chooseLocation` 用于打开地图选择位置。
|
||||
|
||||
## 基础用法
|
||||
|
||||
```javascript
|
||||
uni.chooseLocation({
|
||||
success: (res) => {
|
||||
console.log('选择的位置', res.name, res.address)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## 完整示例
|
||||
|
||||
### 示例 1: 基本选择位置
|
||||
|
||||
```javascript
|
||||
uni.chooseLocation({
|
||||
success: (res) => {
|
||||
console.log('位置名称', res.name)
|
||||
console.log('详细地址', res.address)
|
||||
console.log('纬度', res.latitude)
|
||||
console.log('经度', res.longitude)
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('选择失败', err)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### 示例 2: 在页面中使用
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view class="container">
|
||||
<button @click="chooseLocation">选择位置</button>
|
||||
<view v-if="location" class="location-info">
|
||||
<text>位置:{{ location.name }}</text>
|
||||
<text>地址:{{ location.address }}</text>
|
||||
<text>坐标:{{ location.latitude }}, {{ location.longitude }}</text>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
location: null
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
chooseLocation() {
|
||||
uni.chooseLocation({
|
||||
success: (res) => {
|
||||
this.location = res
|
||||
uni.showToast({
|
||||
title: '选择成功',
|
||||
icon: 'success'
|
||||
})
|
||||
},
|
||||
fail: (err) => {
|
||||
if (err.errMsg.includes('auth deny')) {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '需要位置权限才能选择位置',
|
||||
success: (modalRes) => {
|
||||
if (modalRes.confirm) {
|
||||
uni.openSetting()
|
||||
}
|
||||
}
|
||||
})
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '选择失败',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
```
|
||||
|
||||
### 示例 3: 选择收货地址
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view class="container">
|
||||
<view class="address-item" @click="chooseAddress">
|
||||
<view v-if="address">
|
||||
<text class="address-name">{{ address.name }}</text>
|
||||
<text class="address-detail">{{ address.address }}</text>
|
||||
</view>
|
||||
<view v-else>
|
||||
<text>请选择收货地址</text>
|
||||
</view>
|
||||
<text class="arrow">></text>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
address: null
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
chooseAddress() {
|
||||
uni.chooseLocation({
|
||||
success: (res) => {
|
||||
this.address = {
|
||||
name: res.name,
|
||||
address: res.address,
|
||||
latitude: res.latitude,
|
||||
longitude: res.longitude
|
||||
}
|
||||
// 保存地址
|
||||
uni.setStorageSync('deliveryAddress', this.address)
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
onLoad() {
|
||||
// 加载保存的地址
|
||||
const savedAddress = uni.getStorageSync('deliveryAddress')
|
||||
if (savedAddress) {
|
||||
this.address = savedAddress
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.address-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 20px;
|
||||
border-bottom: 1px solid #eee;
|
||||
}
|
||||
.address-name {
|
||||
display: block;
|
||||
font-size: 32rpx;
|
||||
font-weight: bold;
|
||||
margin-bottom: 10rpx;
|
||||
}
|
||||
.address-detail {
|
||||
display: block;
|
||||
font-size: 28rpx;
|
||||
color: #999;
|
||||
}
|
||||
.arrow {
|
||||
color: #999;
|
||||
}
|
||||
</style>
|
||||
```
|
||||
|
||||
### 示例 4: 检查权限
|
||||
|
||||
```javascript
|
||||
function chooseLocationWithPermission() {
|
||||
// 先检查权限
|
||||
uni.getSetting({
|
||||
success: (res) => {
|
||||
if (res.authSetting['scope.userLocation']) {
|
||||
// 已授权,直接选择
|
||||
chooseLocation()
|
||||
} else {
|
||||
// 请求授权
|
||||
uni.authorize({
|
||||
scope: 'scope.userLocation',
|
||||
success: () => {
|
||||
chooseLocation()
|
||||
},
|
||||
fail: () => {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '需要位置权限才能选择位置',
|
||||
success: (modalRes) => {
|
||||
if (modalRes.confirm) {
|
||||
uni.openSetting()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function chooseLocation() {
|
||||
uni.chooseLocation({
|
||||
success: (res) => {
|
||||
console.log('选择的位置', res)
|
||||
}
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
### 示例 5: 在地图上显示选择的位置
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view class="container">
|
||||
<button @click="chooseLocation">选择位置</button>
|
||||
<map
|
||||
v-if="location"
|
||||
:latitude="location.latitude"
|
||||
:longitude="location.longitude"
|
||||
:markers="markers"
|
||||
class="map"
|
||||
></map>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
location: null,
|
||||
markers: []
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
chooseLocation() {
|
||||
uni.chooseLocation({
|
||||
success: (res) => {
|
||||
this.location = res
|
||||
this.markers = [{
|
||||
id: 1,
|
||||
latitude: res.latitude,
|
||||
longitude: res.longitude,
|
||||
title: res.name
|
||||
}]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.map {
|
||||
width: 100%;
|
||||
height: 500px;
|
||||
margin-top: 20px;
|
||||
}
|
||||
</style>
|
||||
```
|
||||
|
||||
## 返回值
|
||||
|
||||
| 参数名 | 类型 | 说明 |
|
||||
|--------|------|------|
|
||||
| name | String | 位置名称 |
|
||||
| address | String | 详细地址 |
|
||||
| latitude | Number | 纬度 |
|
||||
| longitude | Number | 经度 |
|
||||
|
||||
## 平台兼容性
|
||||
|
||||
| 平台 | 支持情况 |
|
||||
|------|---------|
|
||||
| H5 | ❌ |
|
||||
| 微信小程序 | ✅ |
|
||||
| 支付宝小程序 | ✅ |
|
||||
| 百度小程序 | ✅ |
|
||||
| 字节跳动小程序 | ✅ |
|
||||
| QQ 小程序 | ✅ |
|
||||
| 快手小程序 | ✅ |
|
||||
| App | ✅ |
|
||||
| 快应用 | ✅ |
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. 需要用户授权位置权限
|
||||
2. H5 平台不支持此 API
|
||||
3. 选择的位置包含名称、地址和坐标信息
|
||||
4. 建议在需要时再请求权限
|
||||
|
||||
## 参考资源
|
||||
|
||||
- **官方文档**: https://doc.dcloud.net.cn/uni-app-x/api/location/choose-location.html#chooselocation
|
||||
- **获取位置**: https://doc.dcloud.net.cn/uni-app-x/api/location/location.html#getlocation
|
||||
- **打开地图**: https://doc.dcloud.net.cn/uni-app-x/api/location/open-location.html#openlocation
|
||||
@@ -0,0 +1,243 @@
|
||||
# uni.getLocation - 获取位置示例
|
||||
|
||||
## 官方文档
|
||||
|
||||
参考官方文档:https://doc.dcloud.net.cn/uni-app-x/api/location/location.html#getlocation
|
||||
|
||||
## 概述
|
||||
|
||||
`uni.getLocation` 用于获取当前地理位置。
|
||||
|
||||
## 基础用法
|
||||
|
||||
```javascript
|
||||
uni.getLocation({
|
||||
type: 'wgs84',
|
||||
success: (res) => {
|
||||
console.log('当前位置', res.latitude, res.longitude)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## 完整示例
|
||||
|
||||
### 示例 1: 获取当前位置
|
||||
|
||||
```javascript
|
||||
uni.getLocation({
|
||||
type: 'wgs84',
|
||||
success: (res) => {
|
||||
console.log('纬度', res.latitude)
|
||||
console.log('经度', res.longitude)
|
||||
console.log('速度', res.speed)
|
||||
console.log('精度', res.accuracy)
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('获取位置失败', err)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### 示例 2: 高精度定位
|
||||
|
||||
```javascript
|
||||
uni.getLocation({
|
||||
type: 'gcj02',
|
||||
altitude: true,
|
||||
geocode: true,
|
||||
success: (res) => {
|
||||
console.log('位置信息', res)
|
||||
// res.address 包含地址信息(需要 geocode: true)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### 示例 3: 在页面中使用
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view class="container">
|
||||
<button @click="getCurrentLocation">获取当前位置</button>
|
||||
<view v-if="location" class="location-info">
|
||||
<text>纬度:{{ location.latitude }}</text>
|
||||
<text>经度:{{ location.longitude }}</text>
|
||||
<text>地址:{{ location.address || '未获取' }}</text>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
location: null
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
getCurrentLocation() {
|
||||
uni.showLoading({
|
||||
title: '定位中...'
|
||||
})
|
||||
|
||||
uni.getLocation({
|
||||
type: 'gcj02',
|
||||
geocode: true,
|
||||
success: (res) => {
|
||||
this.location = res
|
||||
uni.hideLoading()
|
||||
uni.showToast({
|
||||
title: '定位成功',
|
||||
icon: 'success'
|
||||
})
|
||||
},
|
||||
fail: (err) => {
|
||||
uni.hideLoading()
|
||||
uni.showToast({
|
||||
title: '定位失败',
|
||||
icon: 'none'
|
||||
})
|
||||
console.error('定位失败', err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
```
|
||||
|
||||
### 示例 4: 检查定位权限
|
||||
|
||||
```javascript
|
||||
// 先检查定位权限
|
||||
uni.getSetting({
|
||||
success: (res) => {
|
||||
if (res.authSetting['scope.userLocation']) {
|
||||
// 已授权,直接获取位置
|
||||
uni.getLocation({
|
||||
type: 'gcj02',
|
||||
success: (res) => {
|
||||
console.log('位置', res)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
// 未授权,请求授权
|
||||
uni.authorize({
|
||||
scope: 'scope.userLocation',
|
||||
success: () => {
|
||||
uni.getLocation({
|
||||
type: 'gcj02',
|
||||
success: (res) => {
|
||||
console.log('位置', res)
|
||||
}
|
||||
})
|
||||
},
|
||||
fail: () => {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '需要定位权限才能使用此功能',
|
||||
showCancel: false
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### 示例 5: 在地图上显示位置
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view class="container">
|
||||
<button @click="getLocationAndShow">获取位置并显示</button>
|
||||
<map
|
||||
v-if="location"
|
||||
:latitude="location.latitude"
|
||||
:longitude="location.longitude"
|
||||
:markers="markers"
|
||||
class="map"
|
||||
></map>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
location: null,
|
||||
markers: []
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
getLocationAndShow() {
|
||||
uni.getLocation({
|
||||
type: 'gcj02',
|
||||
success: (res) => {
|
||||
this.location = res
|
||||
this.markers = [{
|
||||
id: 1,
|
||||
latitude: res.latitude,
|
||||
longitude: res.longitude,
|
||||
title: '我的位置'
|
||||
}]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.map {
|
||||
width: 100%;
|
||||
height: 500px;
|
||||
margin-top: 20px;
|
||||
}
|
||||
</style>
|
||||
```
|
||||
|
||||
## 参数说明
|
||||
|
||||
| 参数名 | 类型 | 必填 | 说明 |
|
||||
|--------|------|------|------|
|
||||
| type | String | 否 | 坐标类型,可选值:wgs84、gcj02 |
|
||||
| altitude | Boolean | 否 | 是否返回高度信息 |
|
||||
| geocode | Boolean | 否 | 是否解析地址信息 |
|
||||
|
||||
## 返回值
|
||||
|
||||
| 参数名 | 类型 | 说明 |
|
||||
|--------|------|------|
|
||||
| latitude | Number | 纬度 |
|
||||
| longitude | Number | 经度 |
|
||||
| speed | Number | 速度 |
|
||||
| accuracy | Number | 位置的精确度 |
|
||||
| altitude | Number | 高度(需要 altitude: true) |
|
||||
| address | Object | 地址信息(需要 geocode: true) |
|
||||
|
||||
## 平台兼容性
|
||||
|
||||
| 平台 | 支持情况 |
|
||||
|------|---------|
|
||||
| H5 | ✅(需要 HTTPS) |
|
||||
| 微信小程序 | ✅ |
|
||||
| 支付宝小程序 | ✅ |
|
||||
| 百度小程序 | ✅ |
|
||||
| 字节跳动小程序 | ✅ |
|
||||
| QQ 小程序 | ✅ |
|
||||
| 快手小程序 | ✅ |
|
||||
| App | ✅ |
|
||||
| 快应用 | ✅ |
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. 需要用户授权定位权限
|
||||
2. H5 平台需要 HTTPS 协议
|
||||
3. `type` 为 `gcj02` 时返回的坐标可用于地图显示
|
||||
4. `geocode` 为 true 时才能获取地址信息
|
||||
|
||||
## 参考资源
|
||||
|
||||
- **官方文档**: https://doc.dcloud.net.cn/uni-app-x/api/location/location.html#getlocation
|
||||
- **打开地图**: https://doc.dcloud.net.cn/uni-app-x/api/location/location.html#openlocation
|
||||
- **选择位置**: https://doc.dcloud.net.cn/uni-app-x/api/location/location.html#chooselocation
|
||||
@@ -0,0 +1,188 @@
|
||||
# uni.openLocation - 打开地图查看位置示例
|
||||
|
||||
## 官方文档
|
||||
|
||||
参考官方文档:https://doc.dcloud.net.cn/uni-app-x/api/location/open-location.html#openlocation
|
||||
|
||||
## 概述
|
||||
|
||||
`uni.openLocation` 用于使用地图查看位置,可以打开系统地图应用显示指定位置。
|
||||
|
||||
## 基础用法
|
||||
|
||||
```javascript
|
||||
uni.openLocation({
|
||||
latitude: 39.908823,
|
||||
longitude: 116.397470,
|
||||
name: '天安门',
|
||||
address: '北京市东城区'
|
||||
})
|
||||
```
|
||||
|
||||
## 完整示例
|
||||
|
||||
### 示例 1: 打开地图查看位置
|
||||
|
||||
```javascript
|
||||
uni.openLocation({
|
||||
latitude: 39.908823,
|
||||
longitude: 116.397470,
|
||||
name: '天安门',
|
||||
address: '北京市东城区天安门广场',
|
||||
scale: 18,
|
||||
success: () => {
|
||||
console.log('打开地图成功')
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('打开地图失败', err)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### 示例 2: 先获取位置再打开地图
|
||||
|
||||
```javascript
|
||||
// 先获取当前位置
|
||||
uni.getLocation({
|
||||
type: 'gcj02',
|
||||
success: (locationRes) => {
|
||||
// 打开地图显示当前位置
|
||||
uni.openLocation({
|
||||
latitude: locationRes.latitude,
|
||||
longitude: locationRes.longitude,
|
||||
name: '我的位置',
|
||||
address: locationRes.address || '当前位置'
|
||||
})
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### 示例 3: 在页面中使用
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view class="container">
|
||||
<button @click="openCurrentLocation">查看当前位置</button>
|
||||
<button @click="openTargetLocation">查看目标位置</button>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
targetLocation: {
|
||||
latitude: 39.908823,
|
||||
longitude: 116.397470,
|
||||
name: '天安门',
|
||||
address: '北京市东城区天安门广场'
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
openCurrentLocation() {
|
||||
uni.getLocation({
|
||||
type: 'gcj02',
|
||||
success: (res) => {
|
||||
uni.openLocation({
|
||||
latitude: res.latitude,
|
||||
longitude: res.longitude,
|
||||
name: '我的位置',
|
||||
address: res.address || '当前位置'
|
||||
})
|
||||
},
|
||||
fail: () => {
|
||||
uni.showToast({
|
||||
title: '获取位置失败',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
})
|
||||
},
|
||||
openTargetLocation() {
|
||||
uni.openLocation({
|
||||
latitude: this.targetLocation.latitude,
|
||||
longitude: this.targetLocation.longitude,
|
||||
name: this.targetLocation.name,
|
||||
address: this.targetLocation.address
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
```
|
||||
|
||||
### 示例 4: 查看商家位置
|
||||
|
||||
```javascript
|
||||
function openStoreLocation(store) {
|
||||
uni.openLocation({
|
||||
latitude: store.latitude,
|
||||
longitude: store.longitude,
|
||||
name: store.name,
|
||||
address: store.address,
|
||||
scale: 18,
|
||||
success: () => {
|
||||
console.log('打开商家位置成功')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 使用
|
||||
const store = {
|
||||
name: '星巴克咖啡',
|
||||
address: '北京市朝阳区xxx路xxx号',
|
||||
latitude: 39.908823,
|
||||
longitude: 116.397470
|
||||
}
|
||||
openStoreLocation(store)
|
||||
```
|
||||
|
||||
### 示例 5: 导航到位置
|
||||
|
||||
```javascript
|
||||
// 在某些平台上,openLocation 可以用于导航
|
||||
uni.openLocation({
|
||||
latitude: 39.908823,
|
||||
longitude: 116.397470,
|
||||
name: '目的地',
|
||||
address: '北京市东城区',
|
||||
scale: 18
|
||||
})
|
||||
```
|
||||
|
||||
## 参数说明
|
||||
|
||||
| 参数名 | 类型 | 必填 | 说明 |
|
||||
|--------|------|------|------|
|
||||
| latitude | Number | 是 | 纬度,范围为 -90~90,负数表示南纬 |
|
||||
| longitude | Number | 是 | 经度,范围为 -180~180,负数表示西经 |
|
||||
| scale | Number | 否 | 缩放比例,范围 5~18,默认为 18 |
|
||||
| name | String | 否 | 位置名称 |
|
||||
| address | String | 否 | 地址的详细说明 |
|
||||
|
||||
## 平台兼容性
|
||||
|
||||
| 平台 | 支持情况 |
|
||||
|------|---------|
|
||||
| H5 | ✅ |
|
||||
| 微信小程序 | ✅ |
|
||||
| 支付宝小程序 | ✅ |
|
||||
| 百度小程序 | ✅ |
|
||||
| 字节跳动小程序 | ✅ |
|
||||
| QQ 小程序 | ✅ |
|
||||
| 快手小程序 | ✅ |
|
||||
| App | ✅ |
|
||||
| 快应用 | ✅ |
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. 需要使用 `gcj02` 类型的坐标(通过 `uni.getLocation` 获取)
|
||||
2. `scale` 值越大,地图显示越详细
|
||||
3. `name` 和 `address` 用于在地图上显示位置信息
|
||||
4. 不同平台打开的地图应用可能不同
|
||||
|
||||
## 参考资源
|
||||
|
||||
- **官方文档**: https://doc.dcloud.net.cn/uni-app-x/api/location/open-location.html#openlocation
|
||||
- **获取位置**: https://doc.dcloud.net.cn/uni-app-x/api/location/location.html#getlocation
|
||||
Reference in New Issue
Block a user