uniapp小程序:使用uni.getLocation通过腾讯地图获取相关地址信息详情(超详细)

news/2024/7/20 2:55:18 标签: uni-app, 小程序

先看运行结果:

流程:

1、在edge网页搜索腾讯位置服务

搜索后点击这里

已经有账号的就进行登录,没有账号的进行注册即可

点击控制台:

进去后点击成员管理---->我的应用---->创建应用

输入相应的参数应用名称(随便写)和应用类型更具你的项目类型进行选择我选择了出行

选择好后点击创建:

创建好后点击添加key:

打开微信小程序开发工具:

这样就获取到了key:

2、下载腾讯小程序JavaScriptJDK点击即可跳转

微信小程序JavaScript SDK | 腾讯位置服务 (qq.com)

下载其中一个都可以

解压后放在common目录下皆可,如果没有common路面自己生成即可。

3、安全域名设计

在微信小程序后台小程序 (qq.com)​​​​​​

添加request合法域名,添加apis.map.qq.com

4、配置manifest.json文件

 "permission" : {
            "scope.userLocation" : {
                "desc" : "为了您更好的体验,请确认获取您的位置"
            }
        },
        "requiredPrivateInfos" : [ "getLocation", "chooseLocation", "chooseAddress" ]

5、引用代码演示

我这里吧方法引入到mixins中

//获取用户实时位置
import QQMapWX from "../../common/qqmap-wx-jssdk.js"

//获取用户实时位置
import QQMapWX from "../../common/qqmap-wx-jssdk.js"
export const showMixin ={
	data(){
		return{
			show: true
		}
	},
	methods:{
		showto(){
			this.show=!this.show
			console.log('this.show',this.show)
			uni.navigateBack({
				delta:1
			})
			
		},
		async getLocationInfo() {
			// this.show = !this.show
			return new Promise((resolve) => {
				let location = {
					longitude: 0,
					latitude: 0,
					province: "",
					city: "",
					area: "",
					street: "",
					address: "",
				};
				uni.getLocation({
					type: "gcj02",
					success(res) {
						location.longitude = res.longitude;
						location.latitude = res.latitude;
		
						const qqmapsdk = new QQMapWX({
							key: 'ANDBZ-VEM6T-IPIXG-VEWUH-XJYGS-N2BPT'
						});
						qqmapsdk.reverseGeocoder({
							location,
							success(response) {
								let info = response.result;
								console.log(info);
								location.province = info.address_component.province;
								location.city = info.address_component.city;
								location.area = info.address_component.district;
								location.street = info.address_component.street;
								location.address = info.address;
								resolve(location);
							}
						});
					},
					fail(err) {
						console.log(err)
					}
				})
			})
		}
	}
}

显示:

引入:

生命周期调用:

代码直接使用即可:

<template>
	<view class="site">
		<view class="map">
			<uni-search-bar class="uni-mt-10" radius="100" placeholder="搜索城市/区县/地点" clearButton="none"
				cancelButton="none" @confirm="search" />
		</view>

		<view class="current">
			<view style="display: flex; font-size: 28rpx; line-height: 44rpx;">
				<uni-icons type="location" size="20"></uni-icons>
				<txte v-if="position !== null">当前位置:{{position}}</txte>
			</view>
			<view style="display: flex; color: #4687e1; font-size: 28rpx;" @click="showto">
				<image src="../../../static/images/tabbar/locations.png" mode="aspectFill"
					style="width: 35rpx; height: 35rpx; margin-right: 10rpx;"></image>
				<text>刷新定位</text>
			</view>

		</view>
		<view class="chosen">
			<view v-for="(item,index) in list" :key="index" class="box" @click="selects(index)"
				:class="{'active': activeindex === index}">
				{{item.name}}
				<view class="line" v-if="activeindex === index"></view>
			</view>
		</view>

		<view class="area">
			<view class="area-box" v-for="(item,index) in box" :key="index" @click="city(index)"
				:class="{'active': activeindexs === index}">
				{{item.name}}
			</view>
		</view>

		<view class="ess">
			<view v-if="activeindexs !== -1" class="area-box" v-for="(item,index) in boxs" :key="index"
				@click="citys(index)" :class="{'active': activeindextwo === index}">
				{{item.name}}
			</view>
		</view>

		<uni-popup ref="popup" background-color="#fff">
			<view style="width: 300rpx; height: 300rpx;">
				<uni-loading></uni-loading>
			</view>
			
		</uni-popup>
	</view>
</template>

<script>
	// import QQMapWX from "../../../common/qqmap-wx-jssdk.js"
	import {
		showMixin
	} from '../../../shopro/mixins/site.js'
	export default {
		mixins: [showMixin],
		data() {
			return {
				position: null,
				activeindex: 0,
				activeindexs: -1,
				activeindextwo: -1,
				list: [{
					name: '贵州省'
				}],
				box: [{
						name: '贵阳市'
					},
					{
						name: '安顺市'
					},
					{
						name: '遵义市'
					},
					{
						name: '毕节市'
					},
					{
						name: '黔东南'
					},
					{
						name: '黔东南'
					},
					{
						name: '黔东南'
					},
					{
						name: '黔东南'
					}
				],
				boxs: [{
						name: '花溪区'
					},
					{
						name: '观山湖区'
					},
					{
						name: '南明区'
					},
					{
						name: '云岩区'
					},
					{
						name: '白云区'
					},
					{
						name: '清镇'
					}
				],

			}
		},
		async mounted() {
			const location = await this.getLocationInfo();
			console.log('location', location)
			// that.position = location.province + location.city
			let position = location.province + location.city + location.area
			console.log('position', position)
			this.position = position

		},
		methods: {
			citys(index) {
				this.activeindextwo = index
			},
			city(index) {
				this.activeindexs = index
			},
			selects(index) {
				this.activeindex = index
				uni.showLoading({
					title:'加载中',
					mask: true
				})
				// this.$refs.popup.open('center')
			}
		},
		// change(e){
		// 	console.log('当前模式:' + e.type + ',状态:' + e.show);
		// }
	}
</script>

<style scoped>
	.area-box {
		width: 130rpx;
		height: 80rpx;
		background-color: #fff;
		text-align: center;
		line-height: 80rpx;
		margin-top: 20rpx;
		margin-right: 10rpx;
		margin-left: 10rpx;
		border-radius: 20rpx;
	}

	.ess {
		width: 100vw;
		height: 300rpx;
		/* 	background-color: #c9c9c9; */
		display: flex;
		justify-content: flex-start;
		align-items: flex-start;
		flex-flow: row wrap;
		align-content: flex-start;
		overflow-y: scroll;
	}

	.area {
		width: 100vw;
		height: 300rpx;
		/* background-color: aqua; */
		display: flex;
		justify-content: flex-start;
		align-items: flex-start;
		flex-flow: row wrap;
		align-content: flex-start;
		overflow-y: scroll;
	}

	.line {
		position: absolute;
		bottom: 10rpx;
		width: 70%;
		height: 5rpx;
		background-color: brown;
		border-radius: 50rpx;
		left: 0;
		right: 0;
		margin: auto;
	}

	.box {
		width: 130rpx;
		height: 100%;
		/* background-color: antiquewhite; */
		text-align: center;
		line-height: 80rpx;
	}

	.active {
		font-weight: bold;
		position: relative;
	}

	.chosen {
		width: 100vw;
		height: 80rpx;
		padding: 0 20rpx;
		background: #f8f8f8;
	}

	.current {
		display: flex;
		align-items: center;
		justify-content: space-between;
		width: 100vw;
		height: 100rpx;
		padding: 0 30rpx;
	}

	.map {
		width: 100vw;
		height: 100rpx;
	}

	.site {
		width: 100vw;
		height: 100vh;
		background-color: #fff;
	}
</style>

运行结果:


http://www.niftyadmin.cn/n/5438609.html

相关文章

autojsx使用

工具&#xff1a; 投屏软件&#xff1a;https://www.sigma-rt.com/tc/download/ autojsx&#xff1a;https://github.com/kkevsekk1/AutoX/releases 开发文档&#xff1a;http://doc.autoxjs.com/#/ 开发工具&#xff1a;https://code.visualstudio.com/ vscode插件&#xff1a…

关于Rust

Rust是一种系统级编程语言&#xff0c;注重安全性、并发性和性能。它由Mozilla开发&#xff0c;旨在提供一种可靠的编程语言&#xff0c;适用于各种应用场景。以下是关于Rust的一些信息12&#xff1a; 1、Rust的特点&#xff1a; 内存安全&#xff1a;Rust使用所有权、借用和…

【Postrsql】postgresql的介绍、安装和使用

介绍 1.基本信息 PostgreSQL是一个功能强大的开源关系型数据库系统。经过长达15年以上的积极开发和不断改进&#xff0c;PostgreSQL已在可靠性、稳定性、数据一致性等获得了业内极高的声誉。目前PostgreSQL可以运行在所有主流操作系统上&#xff0c;包括Linux、Unix和Windows…

微信客服发送欢迎语-用户进入微信客服会话事件

https://developer.work.weixin.qq.com/document/path/94670#%E4%BA%8B%E4%BB%B6%E6%B6%88%E6%81%AF 通过客服链接进入会话&#xff0c;才会触发该事件。需要注意的是&#xff0c;从客服会话列表的已有会话点击进入&#xff0c;不会触发。 进入会话事件&#xff0c;接收到的JS…

遍历list过程中调用remove方法

1、普通for循环遍历List删除指定元素&#xff0c;list.remove(index) List<String> nameList new ArrayList<>(Arrays.asList("张三", "李四", "王五", "赵六")); nameList.add("张七"); nameList.add("…

Spring Cloud 整合 GateWay

目录 第一章 微服务架构图 第二章 Spring Cloud整合Nacos集群 第三章 Spring Cloud GateWay 第四章 Spring Cloud Alibaba 整合Sentinel 第五章 Spring Cloud Alibaba 整合SkyWalking链路跟踪 第六章 Spring Cloud Alibaba 整合Seata分布式事务 第七章 Spring Cloud 集成Auth用…

【LeetCode: 173. 二叉搜索树迭代器 + dfs + 二叉搜索树】

&#x1f680; 算法题 &#x1f680; &#x1f332; 算法刷题专栏 | 面试必备算法 | 面试高频算法 &#x1f340; &#x1f332; 越难的东西,越要努力坚持&#xff0c;因为它具有很高的价值&#xff0c;算法就是这样✨ &#x1f332; 作者简介&#xff1a;硕风和炜&#xff0c;…

获取所有封闭式基金实时行情数据

获取所有封闭式基金实时行情数据&#xff0c;打印完整数据&#xff0c;并保存到csv文件 代码&#xff1a; import pandas as pd import akshare as ak # 列名与数据对其显示 pd.set_option(display.unicode.ambiguous_as_wide, True) pd.set_option(display.unicode.east_asi…