当前位置: 首页 > news >正文

uni-app 获取当前位置的经纬度以及地址信息

文章目录

    • uni.getLocation(objc)
    • 获取经纬度和地址
    • 调试结果
    • 问题

uni-app 获取当前位置的经纬度以及地址信息

uni.getLocation(objc)

uni-app官方文档定位API: uni.getLocation(OBJECT)

uni.getLocation({type: 'wgs84',success: function (res) {console.log('当前位置的经度:' + res.longitude);console.log('当前位置的纬度:' + res.latitude);}
});

获取经纬度和地址

  1. 去高德开放平台注册账号,添加应用,申请个AppKey

  2. 设置

1)配置勾选系统定位和高德定位,配置高德的“用户名”和“app key”
在这里插入图片描述

2)添加定位权限
在这里插入图片描述
<uses-permission android:name="android.permission.LOCATION"/>

  1. 上代码
<template><view class="container"><text class="title">定位示例</text><button @click="getLocation">获取经纬度</button><view v-if="location"><text>纬度: {{ location.latitude }}</text><text>经度: {{ location.longitude }}</text><text>速度: {{ location.speed }} m/s</text><text>精度: {{ location.accuracy }} m</text></view><view v-if="address"><text>位置: {{ address.formatted_address }} </text></view><view v-if="error"><text>错误信息: {{ error.message }}</text></view></view>
</template><script>
export default {data() {return {location: null,address: null,error: null};},methods: {getLocation() {uni.getLocation({type: 'wgs84',success: (res) => {this.location = {latitude: res.latitude,longitude: res.longitude,speed: res.speed,accuracy: res.accuracy,};this.reverseGeocode(res.latitude,res.longitude);console.log('定位成功:', res);},fail: (err) => {this.error = err;console.error('定位失败:', err);}});},// 逆地理编码函数reverseGeocode(latitude, longitude) {const key = '46XXXXXXXXXXXXXXXXXXXXXX0a'; // 换成你自己的AppKeyconst url = `https://restapi.amap.com/v3/geocode/regeo?key=${key}&location=${longitude},${latitude}`;uni.request({url: url,method: 'GET',success: (res) => {if (res.statusCode === 200 && res.data.status === '1') {this.address = {formatted_address: res.data.regeocode.formatted_address,};console.log(res.data.regeocode);} else {console.error('逆地理编码失败:', res.data);}},fail: (error) => {console.error('请求失败:', error);}});}},onLoad() {this.getLocation();}
};
</script><style>
.container {padding: 20px;
}.title {font-size: 20px;font-weight: bold;margin-bottom: 20px;
}button {margin-bottom: 20px;
}text {display: block;margin-bottom: 10px;
}
</style>

1)先调用uni.getLocation(objc) 函数可以拿到经纬度信息
2)再调用高德地图你地理编码的API获取经纬度对应的地址信息。

调试结果

在Safari上可以获取到经纬度和位置信息:

在这里插入图片描述

在Chrome上调试获取不到;

打包apk手机上也可以获取到经纬度和位置信息。

问题

1)uni.getLocation(objc) 这个函数有一个参数geocode

在这里插入图片描述
App上支持这个方法直接获取地址信息,需要设置 geocode 位 true,同时type参数需要设置成gcj02。

    getLocation() {uni.getLocation({type: 'gcj02',geocode: true,success: (res) => {this.location = {latitude: res.latitude,longitude: res.longitude,speed: res.speed,accuracy: res.accuracy,address: res.address,};console.log('定位成功:', res);},fail: (err) => {this.error = err;console.error('定位失败:', err);}});},

我试了试,手机打包可以获取到经纬度,但是位置信息没拿到,不知道是哪里的配置问题,示例如下:

在这里插入图片描述

在这里插入图片描述

http://www.lryc.cn/news/432535.html

相关文章:

  • 【CSS】尺寸单位
  • Agent(智能体)和 MetaGPT,一句话实现整个需求应用代码
  • [数据结构] 哈希结构的哈希冲突解决哈希冲突
  • Wimdows使用Appium IOS自动化
  • C语言深度剖析--不定期更新的第四弹
  • 【手撕数据结构】八大排序神功(上)
  • 【2024高教社杯全国大学生数学建模竞赛】B题模型建立求解
  • OpenHarmony鸿蒙开发( Beta5.0)智能手表应用开发实践
  • 共享单车轨迹数据分析:以厦门市共享单车数据为例(一)
  • SprinBoot+Vue在线商城微信小程序的设计与实现
  • 4--SpringBootWeb-请求响应
  • 电脑点击关机之后,又自动重启开机了。根本就关不了?
  • 强化网络安全:通过802.1X协议保障远程接入设备安全认证
  • 链动2+1模式AI智能名片S2B2C商城小程序源码在社群商业价值构建中的应用探索
  • 基于SpringBoot+Vue+MySQL的校园周边美食探索及分享平台
  • “设计模式双剑合璧:工厂模式与策略模式在支付系统中的完美结合”
  • 第二百一十九节 JPA 教程 - JPA 字段映射示例
  • 目标检测-YOLOv6
  • Java面向对象与多态
  • redis分布式锁和lua脚本
  • 项目实战 ---- 商用落地视频搜索系统(5)---service层核心
  • Win32远线程注入
  • CTF 竞赛密码学方向学习路径规划
  • 2024数学建模国赛B题代码
  • PyTorch 卷积层详解
  • 【Kubernetes知识点问答题】kubernetes 控制器
  • Patlibc———更快捷的更换libc
  • 基于飞腾平台的Hive的安装配置
  • c# json使用
  • 单点登录:cas单点登录实现原理浅析