惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
A
Arctic Wolf
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
T
The Exploit Database - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
L
Lohrmann on Cybersecurity
Martin Fowler
Martin Fowler
A
About on SuperTechFans
P
Palo Alto Networks Blog
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
博客园_首页
大猫的无限游戏
大猫的无限游戏
Cisco Talos Blog
Cisco Talos Blog
P
Proofpoint News Feed
D
DataBreaches.Net
Cyberwarzone
Cyberwarzone
T
Tor Project blog
IT之家
IT之家
P
Proofpoint News Feed
Help Net Security
Help Net Security
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
CXSECURITY Database RSS Feed - CXSecurity.com
Microsoft Azure Blog
Microsoft Azure Blog
V2EX - 技术
V2EX - 技术
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
B
Blog
N
News and Events Feed by Topic
The Cloudflare Blog
S
Schneier on Security
P
Privacy & Cybersecurity Law Blog
T
The Blog of Author Tim Ferriss
Recorded Future
Recorded Future
Last Week in AI
Last Week in AI
The Last Watchdog
The Last Watchdog
Hacker News - Newest:
Hacker News - Newest: "LLM"
L
LangChain Blog
I
InfoQ
F
Full Disclosure
The Register - Security
The Register - Security
阮一峰的网络日志
阮一峰的网络日志
H
Hacker News: Front Page
V
V2EX

吖远zzyの博客

记录一下豆包视频去水印的研究过程,以及fallback_api加密数据qAAB开头数据的解密。 | 吖远zzyの博客 解决华为开发者工具 DevEco Studio 登录跳转 localhost:10101 端口被阻止问题 | 吖远zzyの博客 微信小程序个人资质审核血泪史:踩坑,从反复被拒到神奇过审 | 吖远zzyの博客 如何实现公众号自动回复或做一个短视频无水印解析机器人? | 吖远zzyの博客 以后在手机上用 Termux 和 GitHub Actions 更新 Hexo 博客 | 吖远zzyの博客 必备!这款万能小程序能给抖音/豆包视频图集去除水印、还有有趣的测反应、玩成语模拟工具。 自制的一些免费API接口第二弹(获取QQ昵称、头像、抖音/豆包视频去水印等...) 告别原生导航栏:微信小程序自定义导航栏完美适配方案 在安卓手机上运行OpenClaw?当然可以,用Termux折腾OpenClaw安装QQ机器人的踩坑记录... | 吖远zzyの博客 个人开发工具之抖音直播录制工具:一款功能强大的Android直播录制应用 UniApp中Canvas绘图的易错点与踩坑指南 QQ频道机器人与UniApp开发:常见踩坑点与解决方案 UniApp中Canvas绘图不显示的常见问题与解决方案 Hexo博客实现随机文章功能的完整教程 QQ频道机器人Android客户端使用指南 | 吖远zzyの博客 安卓版QQ频道机器人APP客户端插件开发指南 | 吖远zzyの博客 2024年AI编程助手深度评测:哪款最适合你? uni-app文件上传踩坑记:图片处理和上传全攻略 uni-app表单验证踩坑记:这些坑我替你踩过了 uni-app开发踩坑记录:新手必看的常见问题与解决方案 uni-app安全防护指南:构建可靠的跨端应用 uni-app性能优化指南:从加载到渲染的全方位提升 uni-app动画效果实战:从基础到高级的动画实现指南 uni-app组件开发实战:从基础到进阶的最佳实践 uni-app网络请求与缓存策略:构建高效的数据层 uni-app状态管理进阶:Vuex最佳实践与性能优化 uni-app路由与页面跳转:最佳实践与踩坑指南
uni-app地图定位踩坑记:地图功能和定位的那些坑
吖远zzy · 2024-06-25 · via 吖远zzyの博客
  1. 1. 1. 定位权限问题
    1. 1.1. 1.1 获取定位权限
    2. 1.2. 1.2 定位失败处理
  2. 2. 2. 地图显示问题
    1. 2.1. 2.1 地图加载失败
    2. 2.2. 2.2 标记点显示问题
  3. 3. 3. 路线规划问题
    1. 3.1. 3.1 路线绘制错误
    2. 3.2. 3.2 导航问题
  4. 4. 4. 性能优化问题
    1. 4.1. 4.1 标记点过多
    2. 4.2. 4.2 频繁更新
  5. 5. 5. 实用功能分享
    1. 5.1. 5.1 计算距离
    2. 5.2. 5.2 地址解析
  6. 6. 6. 最佳实践建议
  7. 7. 7. 总结

uni-app地图定位踩坑记:地图功能和定位的那些坑 0 次阅读

在uni-app开发中,地图和定位功能经常会遇到各种问题。本文总结了一些常见的坑和解决方案,帮助大家更好地实现地图相关功能。

1. 定位权限问题

1.1 获取定位权限

问题描述:
用户拒绝定位权限后,无法正常使用定位功能。

解决方案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 检查定位权限
async checkLocationAuth() {
try {
const res = await uni.getSetting()
if (!res.authSetting['scope.userLocation']) {
// 未授权,引导用户开启
await uni.showModal({
title: '提示',
content: '需要获取您的位置信息,是否授权?',
success: async (res) => {
if (res.confirm) {
await uni.openSetting()
}
}
})
}
return res.authSetting['scope.userLocation']
} catch (e) {
console.error('获取权限失败:', e)
return false
}
}

1.2 定位失败处理

问题描述:
定位失败时没有合适的提示和处理。

解决方案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 获取位置信息
async getLocation() {
try {
const auth = await this.checkLocationAuth()
if (!auth) {
uni.showToast({
title: '请先开启定位权限',
icon: 'none'
})
return null
}

const location = await uni.getLocation({
type: 'gcj02', // gcj02火星坐标系
isHighAccuracy: true, // 开启高精度定位
highAccuracyExpireTime: 3000 // 超时时间
})

return location
} catch (e) {
let message = '定位失败'
switch (e.errMsg) {
case 'getLocation:fail system permission denied':
message = '系统定位权限已关闭'
break
case 'getLocation:fail timeout':
message = '定位超时,请重试'
break
default:
message = '定位失败,请检查网络'
}

uni.showToast({
title: message,
icon: 'none'
})
return null
}
}

2. 地图显示问题

2.1 地图加载失败

问题描述:
地图组件加载失败或显示空白。

解决方案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!-- 地图组件使用 -->
<template>
<view class="map-container">
<map
id="map"
:style="mapStyle"
:latitude="latitude"
:longitude="longitude"
:markers="markers"
:scale="scale"
@regionchange="onRegionChange"
@markertap="onMarkerTap"
:show-location="true"
></map>
</view>
</template>

<script>
export default {
data() {
return {
mapStyle: 'width: 100%; height: 300px;',
latitude: 0,
longitude: 0,
markers: [],
scale: 16
}
},
async onReady() {
// 等待地图组件创建完成
this.mapContext = uni.createMapContext('map', this)
await this.initMap()
},
methods: {
async initMap() {
try {
const location = await this.getLocation()
if (location) {
this.latitude = location.latitude
this.longitude = location.longitude
// 添加当前位置标记
this.addMarker({
id: 0,
latitude: location.latitude,
longitude: location.longitude,
title: '当前位置'
})
}
} catch (e) {
console.error('初始化地图失败:', e)
}
}
}
}
</script>

2.2 标记点显示问题

问题描述:
自定义标记点样式不生效或显示异常。

解决方案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 添加自定义标记
function addMarker(point) {
const marker = {
id: point.id,
latitude: point.latitude,
longitude: point.longitude,
title: point.title,
width: 32,
height: 32,
callout: {
content: point.title,
color: '#ffffff',
fontSize: 14,
borderRadius: 4,
bgColor: '#1989fa',
padding: 8,
display: 'ALWAYS'
},
// 自定义图标
iconPath: point.iconPath || '/static/images/marker.png',
// 自定义气泡
customCallout: {
display: 'ALWAYS',
anchorY: 0,
anchorX: 0
}
}

this.markers.push(marker)
}

3. 路线规划问题

3.1 路线绘制错误

问题描述:
路线规划时绘制的路线不准确或无法显示。

解决方案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 规划路线
async planRoute(start, end) {
try {
// 清除旧路线
this.polyline = []

// 获取路线规划
const route = await this.getRoutePoints(start, end)
if (!route) return

// 绘制路线
this.polyline = [{
points: route,
color: '#1989fa',
width: 4,
arrowLine: true,
borderColor: '#ffffff',
borderWidth: 2
}]

// 调整视野以显示整条路线
this.mapContext.includePoints({
points: [start, end],
padding: [50, 50, 50, 50]
})
} catch (e) {
console.error('规划路线失败:', e)
}
}

3.2 导航问题

问题描述:
调用导航功能时出现兼容性问题。

解决方案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// 打开导航
async openNavigation(destination) {
try {
// 检查平台
// #ifdef APP-PLUS
// APP端使用系统导航
plus.runtime.openURL(
`androidamap://navi?sourceApplication=appname&lat=${destination.latitude}&lon=${destination.longitude}&dev=0&style=2`
)
// #endif

// #ifdef H5
// H5端使用Web导航
window.location.href = `https://uri.amap.com/navigation?to=${destination.longitude},${destination.latitude}&mode=car&policy=1`
// #endif

// #ifdef MP-WEIXIN
// 小程序端使用微信导航
uni.openLocation({
latitude: destination.latitude,
longitude: destination.longitude,
name: destination.name,
address: destination.address
})
// #endif
} catch (e) {
uni.showToast({
title: '打开导航失败',
icon: 'none'
})
}
}

4. 性能优化问题

4.1 标记点过多

问题描述:
地图上标记点太多导致卡顿。

解决方案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 聚合标记点
function clusterMarkers(markers, distance = 20) {
const clusters = []
const used = new Set()

markers.forEach((marker, index) => {
if (used.has(index)) return

const cluster = {
center: {
latitude: marker.latitude,
longitude: marker.longitude
},
points: [marker]
}

// 查找附近的点
markers.forEach((m, i) => {
if (i === index || used.has(i)) return

const d = this.calculateDistance(
marker.latitude,
marker.longitude,
m.latitude,
m.longitude
)

if (d <= distance) {
cluster.points.push(m)
used.add(i)
}
})

clusters.push(cluster)
})

return clusters
}

4.2 频繁更新

问题描述:
频繁更新标记点位置导致性能问题。

解决方案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 使用节流更新位置
const throttleUpdate = throttle(function(markers) {
this.markers = markers
}, 200)

// 更新标记位置
function updateMarkers(newMarkers) {
throttleUpdate(newMarkers)
}

// 节流函数
function throttle(fn, delay) {
let timer = null
return function(...args) {
if (timer) return
timer = setTimeout(() => {
fn.apply(this, args)
timer = null
}, delay)
}
}

5. 实用功能分享

5.1 计算距离

1
2
3
4
5
6
7
8
9
10
11
12
13
// 计算两点距离
function calculateDistance(lat1, lng1, lat2, lng2) {
const R = 6371 // 地球半径,单位km
const dLat = (lat2 - lat1) * Math.PI / 180
const dLng = (lng2 - lng1) * Math.PI / 180

const a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
Math.sin(dLng/2) * Math.sin(dLng/2)

const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
return R * c
}

5.2 地址解析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 地址转坐标
async geocode(address) {
try {
const res = await uni.request({
url: 'geocode-api-url',
data: {
address: address,
key: 'your-api-key'
}
})

if (res.data.status === '1') {
return {
latitude: res.data.result.location.lat,
longitude: res.data.result.location.lng
}
}
return null
} catch (e) {
console.error('地址解析失败:', e)
return null
}
}

6. 最佳实践建议

  1. 注意权限处理
  2. 做好错误提示
  3. 优化性能表现
  4. 处理兼容问题
  5. 注意用户体验

7. 总结

  1. 权限问题要提前处理
  2. 地图显示要做好兼容
  3. 路线规划要考虑性能
  4. 导航功能要适配平台
  5. 注意优化用户体验

如果觉得文章对你有帮助,欢迎点赞、评论、分享,你的支持是我继续创作的动力!