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

uniapp相关地图 API调用

目录

一、 注意事项: manifest.json需增加配置

二、获取用户收货地址 [uni.chooseAddress]

三、获取当前的地理位置、速度 [uni.getLocation]

四、打开地图选择位置、查看位置(导航) [uni.chooseLocation] [uni.openLocation]

五、使用腾讯地图逆地址解析接口实现城市自动定位回显


一、 注意事项: manifest.json需增加配置
"mp-weixin": {"requiredPrivateInfos": ["chooseLocation", "getLocation", "getLocation","chooseAddress"],"permission": {"scope.userLocation": {"desc": "你的位置信息将用于小程序位置接口的效果展示"}}
},
二、获取用户收货地址 [uni.chooseAddress]

API接口参考:https://uniapp.dcloud.net.cn/api/other/choose-address.html#chooseaddress

1、效果图

2、vue页面代码

<template><view><u-icon size="12" name="arrow-down" label="选择地址" labelPos="left"@click="citySelect()"></u-icon></view>
</template><script>export default {data() {return {}},methods: {citySelect(){uni.chooseAddress({type: 'wgs84', success: (res) => {console.log(res)},fail: (err) => {console.error('获取位置失败:', err);}})},}};
</script><style>page {height: 100%;background: white;}.homePage {padding-bottom: 5px;}
</style>
三、获取当前的地理位置、速度 [uni.getLocation]

API接口参考:https://uniapp.dcloud.net.cn/api/location/location.html#getlocation

1、调用方式

uni.getLocation({type: 'wgs84', // 返回可以用于uni.openLocation的经纬度,默认为wgs84的gps坐标success: (res) => {console.log(res)},fail: (err) => {console.error('获取位置失败:', err);}
});

2、res结果

{  "latitude": 29.56471,  "longitude": 106.55073,  "speed": -1,  "accuracy": 65,  "verticalAccuracy": 65,  "horizontalAccuracy": 65,  "errMsg": "getLocation:ok"  
}
四、打开地图选择位置、查看位置(导航) [uni.chooseLocation] [uni.openLocation]

API接口参考:

https://uniapp.dcloud.net.cn/api/location/location.html#chooselocation

https://uniapp.dcloud.net.cn/api/location/open-location.html#openlocation

1、效果图

    

2、子组件代码

<template><view><map id="map" longitude="116.397470" latitude="39.908823" scale="14" @tap="chooseLocation"style="width: 100%; height: 300px;"></map><!-- <view v-if="location">位置:{{ location.address }}<button @click="navigateTo">导航到这里</button></view> --></view>
</template><script>export default {data() {return {location: null}},methods: {chooseLocation() {const that = this;uni.chooseLocation({success(res) {that.location = {latitude: res.latitude,longitude: res.longitude,address: res.address,};that.callParent();},fail(err) {console.log('选择位置失败:', err);},});},callParent() {//传值回父组件this.$emit('parentMethod', this.location);},navigateTo() {if (this.location) {uni.openLocation({latitude: this.location.latitude,longitude: this.location.longitude,address: this.location.address,success() {console.log('导航成功');},fail(err) {console.log('导航失败:', err);},});}},}}
</script><style>
</style>

3、父组件使用子组件

<template><view class="box"><!-- 注意,如果需要兼容微信小程序,最好通过setRules方法设置rules规则 --><u--form labelWidth="120" labelPosition="top" labelAlign="left" :labelStyle="{'fontSize':'14px'}" :model="form"<u-form-item label="位置信息" prop="location"><view>{{location.address}}</view><map-select @parentMethod="callLocation"></map-select></u-form-item></u--form></view>
</template><script>import MapSelect from "@/pages/assembly/MapSelect.vue";export default {components: {'map-select': MapSelect},data() {return {form:{},location:{}};},methods: {callLocation(location){console.log("父组件print:",location);this.location=location;}},};
</script><style>page {height: 100%;background: white;}.box {margin: 10px 20px 20px 20px;}.confirmButton {padding-bottom: 50px;}.u-form-item__body__left.data-v-5e7216f1 {position: relative;}.u-form-item__body__left__content.data-v-5e7216f1 {position: absolute;top: 0;}
</style>

4、res结果

{  "errMsg": "chooseLocation:ok",  "name": "雾都宾馆",  "address": "重庆市渝中区上曾家岩24号",  "latitude": 29.565184,  "longitude": 106.551766  
}
五、使用腾讯地图逆地址解析接口实现城市自动定位回显

uniapp中腾讯地图SDK-安装及配置(自动定位回显城市)-CSDN博客 

http://www.xdnf.cn/news/15926.html

相关文章:

  • servicemesh 学习
  • 实战分享:Web3 前端开发Defi项目
  • [硬件电路-39]:激光光路的光信号处理、模拟电路的电信号处理、数字电路的电信号处理、软件的信号处理,有哪些共通的操作、运算、变换?
  • 06-人机共生:Prompt之外的思考
  • 【RK3576】【Android14】USB开发调试
  • k8s 基本架构
  • 【小沐学GIS】基于Rust绘制三维数字地球Earth(Rust、OpenGL、GIS)
  • 完美解决 Ubuntu 中自定义启动器图标重复的问题(以 MATLAB 为例)
  • bash方式启动模型训练
  • python基础复习
  • 高压电工作业证考试核心考点:电气安全基础篇
  • 响应式单位rpx及搭配使用UI产品工具
  • 风格多样!5 个覆盖全风格的素材网站,创作有新意
  • AUTOSAR进阶图解==>AUTOSAR_SWS_DiagnosticOverIP
  • 创建套接字并bind的详细过程
  • 从 Server.xml 到字节码:Tomcat 内核全景与请求旅程 10 000 字深剖
  • MinIO深度解析:从核心特性到Spring Boot实战集成
  • 数据结构与算法之美:拓扑排序
  • 外观设计模式
  • Uniapp之键盘弹窗
  • win10连接鼠标自动关闭触摸板/win10关闭触摸板(笔记本)
  • 智能合约代理与批量调用优化:最小代理与MultiCall的应用
  • android studio libs.versions.toml 配置
  • 嵌入式硬件中电感的基本原理与实现详解
  • CSS篇——第二章 六十五项关键技能(下篇)
  • Kotlin方差
  • OpenCV 官翻5 - 机器学习
  • 智能制造——解读39页汽车行业数字化工厂解决方案【附全文阅读】
  • 考研408《计算机组成原理》复习笔记,第三章(5)——磁盘存储器
  • 文生图-StoryGAN:用于故事可视化的顺序条件GAN