uniapp实现城市列表选择获取经纬度、附带搜索功能(移动端、微信小程序)

准备工作

所用到的技术

  • 腾讯地图微信小程序SDK
  • 高德地图WebServiceAPI服务
  • APP获取是否授权插件
  • uview框架(不是必须)

接下来带大家去申请

腾讯地图微信小程序SDK

微信小程序JavaScript SDK
点击下载 JavaScriptSDK v1.2
然后去申请腾讯地图的 key

先创建应用 > 在添加key
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在应用列表中就能看到我们申请的 key
在这里插入图片描述

高德地图WebServiceAPI服务

高德地图开放平台
高德地图这里只需要去申请一个 WebServiceAPI 服务的key
申请流程和腾讯地图一样
先创建应用 > 在添加key
在这里插入图片描述
在这里插入图片描述

APP获取是否授权插件

dcloud 插件市场添加 插件链接
直接使用 HBuilderX 导入到项目
选择自己的项目即可
在这里插入图片描述
在这里插入图片描述

uview框架

uview 框架这里就不写操作步骤了
大家根据步骤安装就行了

代码实现

两个页面 index.vue fixedPosition.vue

首页index.vue代码

<template>
	<view class="content">
		<button class="" @click="positionClick">点击选择位置</button>
		<view class="">当前地址:{{address.city}}{{address.area}}</view>
		<view class="">当前位置坐标:{{address.lng}},{{address.lat}}</view>
	</view>
</template>
<script>
	import QQMapWX from '@/utils/qqmap-wx-jssdk.min.js' // 腾讯地图SDK (自己把腾讯地图SDK添加到自己指定的文件)
	export default {
		data() {
			return {
				address: {
					city: '',// 城市
					area: '',// 区、县
					lng: '',// 经度
					lat: '' // 纬度
				}
			}
		},
		onLoad() {
			// 位置授权
			this.getLocation()
		},
		onShow() {
			let _this = this
			// 重新获取定位
			uni.$on('againLocation', function() {
				_this.address = {
					city: '',
					area: '',
					lng: '',
					lat: ''
				}
				_this.getLocation()
			})
			// 解析传过来的位置
			uni.$on('getLocation', function(city, area) {
				let address = city + area
				_this.getMapAddress(address)
			})
		},
		mounted() {

		},
		methods: {
			// 选择位置
			positionClick() {
				let item = encodeURIComponent(JSON.stringify(this.address))
				uni.navigateTo({
					url: `/pages/fixedPosition/index?item=${item}`
				})
			},
			// 重新获取定位
			getLocation() {
				let _this = this
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						_this.getAddress(res.longitude, res.latitude).then(res => {
							_this.address = {
								city: res.result.ad_info.city,
								area: res.result.ad_info.district,
								lng: res.result.ad_info.location.lng,
								lat: res.result.ad_info.location.lat
							}
						}).catch(res => {})
					},
					fail() {
						// 首次拒绝授权位置(根据自己要求配置或不配默认地址)
						_this.address = {
							city: '北京市',
							area: '',
							lng: '116.407526',
							lat: '39.904030'
						}
					}
				});
			},
			// 高德位置解析
			getMapAddress(address) {
				let _this = this
				return new Promise(function(resolve, reject) {
					uni.request({
						url: `https://restapi.amap.com/v3/geocode/geo?address=${ address }&key=这里填高德申请的key`,
						method: "GET",
						success(res) {
							console.info("高德地图", res)
							if (res.data.status == 1) {
								const coordinate = res.data.geocodes[0].location.split(",")
								_this.address = {
									city: res.data.geocodes[0].city,
									area: res.data.geocodes[0].district.length != 0 ? res.data.geocodes[0].district : "",
									lng: coordinate[0],
									lat: coordinate[1],
								}
							}
							resolve();
						},
					});
				});
			},
			// 腾讯地图经纬度转中文地址
			getAddress(lng, lat) {
				return new Promise((resove, reject) => {
					const qqmapsdk = new QQMapWX({
						key: '腾讯地图申请的key', //之前在腾讯位置服务申请的key
					})
					qqmapsdk.reverseGeocoder({
						location: {
							latitude: lat,
							longitude: lng,
						},
						success: (res) => {
							resove(res)
						},
						fail: (e) => {
							reject(e)
						},
					})
				})
			}
		}
	}
</script>
<style lang="scss" scoped>
	.content {}
</style>

城市列表fixedPosition.vue页面

<template>
	<view class="fixed-position">
		<!-- 城市搜索 -->
		<view class="search">
			<input placeholder="请输入城市" border="surround" v-model="code" shape="circle" prefixIcon="search" clearable
				@focus="inputFocus" @blur="inputBlur" @input="inputFun"></input>
			<view class="cancel" v-show="isCancel" @click="inputCancel">取消</view>
		</view>
		<!-- 搜索结果模块 -->
		<view class="search-box" v-show="isFocus || isCancel">
			<view class="search-item" v-for="(item,index) in searchList" :key="index" @click="getLocation(item.name)">
				<view class="">{{item.name}}</view>
			</view>
		</view>
		<!-- 城市、区县切换tabs 就这里用到了uview框架 -->
		<u-tabs :list="tabsList" :current="current" @click="tabsClick" v-show="!isCancel"></u-tabs>
		<view v-show="current == 0">
			<!-- 当前位置、重新定位功能 -->
			<view class="current-location" v-show="!isCancel">
				<view class="left">当前:{{area.city}}{{area.area}}</view>
				<view class="right" @click="Repositioning"><u-icon name="map" style="margin-right: 10rpx;"></u-icon>重新定位
				</view>
			</view>
			<!-- 右侧索引 -->
			<view class='letter_right' v-show="!isCancel">
				<text class='letter_item' v-for='(item,index) in letter' :key='index'
					@tap.stop='letterTap(index)'>{{item}}</text>
			</view>
			<!-- 城市列表 -->
			<scroll-view scroll-with-animation="true" class="cityListView" scroll-y="true" :scroll-into-view="scrollId"
				v-show="!isCancel">
				<view class="city-index-list">
					<view class="city-item" v-for="(item,index) in cityList" :key="index" :id="item.letter">
						<view class="letter">{{item.letter}}</view>
						<view class="city" v-for="(text) in item.city" @click="getLocation(text.name)">{{text.name}}</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<!-- 区县展示 -->
		<view class="area" v-show="current == 1">
			<view class="area-list">
				<view class="area-item" v-for="(item,index) in areaList" :key="index">
					<view class="area-title" @click="areaLocation(item.name)">{{item.name}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { city } from './city.js' // 结尾有下载地址
	import { area } from './area.js' // 结尾有下载地址
	import permision from "@/js_sdk/wa-permission/permission.js" // 导入从DCloud安装的插件
	export default {
		data() {
			return {
				address: {
					city: '',
					cityId: ''
				},
				code: '',
				scrollId: '',
				letter: ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T","U", "V", "W", "X", "Y", "Z"],
				cityList: city,
				cityAll: [],
				tabsList: [{name: '国内'}, {name: ''}],
				areaList: [],
				areaAll: [],
				searchList: [],
				current: 0,
				area: {
					city: '',
					area: ''
				},
				isFocus: false,
				isCancel: false
			}
		},
		onLoad(options) {
			// 首页传过来的数据
			let item = JSON.parse(decodeURIComponent(options.item))
			let name = item.city.split("市").join("");
			this.cityList.forEach(itemS => {
				itemS.city.forEach(text => {
					if (text.name === item.city) {
						this.address.city = text.name
						this.address.cityId = text.id
					}
				})
			})
			// 根据城市ID查找出改城市所属的区县
			this.areaList = area[this.address.cityId]
			this.area.city = item.city
			this.area.area = item.area
			// 初始tabs标题
			this.tabsList[1].name = name + '区县'
		},
		async onShow() {
			let isLocations = false
			let _this = this
			let device = uni.getSystemInfoSync().platform // 获取当前设备
			if (device === 'android') {
				isLocations = await permision.requestAndroidPermission("android.permission.ACCESS_FINE_LOCATION") // 安卓方法返回用户是否授权获取位置
			} else if (device === 'ios') {
				isLocations = permision.judgeIosPermission("location") // ios方法返回用户是否授权获取位置
			} else {
				isLocations = await this.weChatGetLocation() // 返回微信小程序是否授权了位置
			}
			// 未授权时弹窗提示用户授权
			if (!isLocations) {
				uni.showModal({
					title: '提示',
					content: '请开启位置信息权限',
					showCancel: false,
					success() {
						// 设备为android或ios是跳转系统设置(用户自行设置)
						if (device === 'android' || device === 'ios') {
							permision.gotoAppPermissionSetting(); // 打开权限设置界面
							uni.navigateBack()
						} else {
							// 微信小程序时打开小程序设置界面
							uni.openSetting({
								success: (res) => {}
							})
						}
					}
				});
			}
		},
		mounted() {
			// 初始化城市JS数据
			this.cityList.forEach(item => {
				this.cityAll = this.cityAll.concat(item.city)
			})
		},
		onUnload() {
			// 销毁事件监听
			uni.$off('againLocation')
			uni.$off('getLocation')
		},
		methods: {
			// 重新定位
			Repositioning() {
				uni.$emit('againLocation')
				uni.navigateBack()
			},
			// 获取定位
			getLocation(item) {
				let name = item
				uni.$emit('getLocation', name)
				uni.navigateBack()
			},
			// 区定位
			areaLocation(item) {
				uni.$emit('getLocation', this.area.city, item)
				uni.navigateBack()
			},
			// 右边索引点击
			letterTap(index) {
				this.scrollId = this.letter[index]
			},
			// tabs状态切换
			tabsClick(item) {
				this.current = item.index
			},
			// 搜索输入框聚焦
			inputFocus() {
				this.isFocus = true
				this.isCancel = true
			},
			// 搜索输入框失去焦点
			inputBlur() {
				this.isFocus = false
			},
			// 搜索框取消
			inputCancel() {
				this.isCancel = false
			},
			// 搜索输入框输入事件
			inputFun(e) {
				let value = e
				let newArray = []
				if (value == '') {
					this.searchList = []
				} else {
					for (let i = 0; i < this.cityAll.length; i++) {
						if (this.cityAll[i].name.indexOf(value) != -1 || this.cityAll[i].letter == value.toUpperCase()) {
							newArray.push(this.cityAll[i])
						}
					}
					this.searchList = newArray
				}
			},
			// 微信小程序是否授权位置信息
			weChatGetLocation() {
				return new Promise((resolve, reject) => {
					uni.authorize({
						scope: 'scope.userLocation',
						success() {
							resolve(true)
							console.log('授权了')
						},
						fail() {
							resolve(false)
							console.log('没有授权')
						}
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.fixed-position {
		background-color: #fff;

		.search {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx 30rpx;
			padding-bottom: 0rpx;

			.cancel {
				margin-left: 30rpx;
			}
		}

		.search-box {
			width: 100vw;
			height: calc(100vh - 96rpx);

			.search-item {
				height: 88rpx;
				line-height: 88rpx;
				padding-left: 30rpx;
				border-bottom: 1rpx solid #eee;
				display: flex;
				align-items: center;
			}
		}

		.current-location {
			padding: 30rpx 30rpx;
			background-color: #fff;
			display: flex;
			justify-content: space-between;

			.right {
				display: flex;
				align-items: center;

				::v-deep .u-icon--right {
					margin-right: 10rpx;
				}
			}
		}

		.letter_right {
			z-index: 99;
			width: 60rpx;
			display: flex;
			height: 100%;
			position: fixed;
			right: 0;
			flex-direction: column;
			justify-content: center;
			top: 60rpx;

			.letter_item {
				display: block;
				font-size: 20rpx;
				color: #666;
				text-align: center;
				padding-left: 20rpx;
				padding-top: 3rpx;
				padding-bottom: 3rpx;
				font-weight: bold;
			}
		}

		.cityListView {
			height: calc(100vh - 284rpx);

			.city-index-list {
				.city-item {
					.letter {
						height: 88rpx;
						line-height: 88rpx;
						font-weight: bold;
						border-bottom: 1rpx solid #eee;
						padding-left: 30rpx;
					}

					.city {
						height: 88rpx;
						line-height: 88rpx;
						padding-left: 30rpx;
						border-bottom: 1rpx solid #eee;
					}
				}
			}
		}

		.area {
			.area-list {
				display: flex;
				flex-wrap: wrap;
				padding: 30rpx;

				.area-item {
					width: 33%;
					height: 110rpx;
					text-align: center;

					.area-title {
						width: 70%;
						margin: 0 auto;
						height: 68rpx;
						line-height: 68rpx;
						border: 1rpx solid #eee;
						background: #fff;
						border-radius: 10rpx;
						text-overflow: ellipsis;
						white-space: nowrap;
						overflow: hidden;
						padding: 0 20rpx;
					}
				}
			}
		}
	}
</style>

注意(必看)

APP:如果你的APP时在开发阶段需要打开 定位功能
在项目的 manifest.json 中的 APP模块配置
在这里插入图片描述
有问题可以留言也可以私我。
到这里也就结束了,谢谢大家观看。

城市、区县数据

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在uniapp实现获取微信授权登录功能,需要进行以下步骤: 1. 首先,在微信公众平台中注册账号,并进入开发者中心,创建小程序并获取小程序的APP ID。 2. 在uniapp中引入并使用uni.login()接口,以获取用户的登录凭证code。 3. 接着,使用uni.request()或uni.requestPayment()接口向微信服务器发送请求,将code和APP ID传递给微信服务器,并获取用户的openid等信息。 4. 对于用户已授权的情况,可以在uni.login()的success回调函数中获取用户信息和用户信息签名,并进行相关处理。 5. 对于用户未授权的情况,需要使用uni.authorize()接口主动向用户请求获得授权,并在其回调函数中进行用户信息的获取和处理。 总的来说,通过在uniapp中调用微信API实现了微信授权登录,可以方便地获取用户的openid等信息,并基于此实现个性化的小程序应用。 ### 回答2: 如今,微信小程序已经成为搭建企业或商家自有移动应用的重要渠道,而Uniapp则成为了众多开发者、 IT公司使用的一款优秀的跨平台开发框架,在Uniapp平台下实现微信授权登录功能,也成为了开发者的必备技能。接下来,本文将围绕如何利用Uniapp开发小程序并实现获取微信授权登录功能进行探讨。 首先,我们在小程序管理后台进行小程序账户的设置操作,具体操作如下: 1. 开发设置-开发权限,将“网页授权获取用户基本信息”权限打开; 2. 个人资质认证,将小程序认证为个人小程序,然后在“认证信息”页面中进行身份证和联系方式的填写。 接下来,我们就可以正式开始使用Uniapp进行小程序开发以及微信授权登录的功能实现,步骤如下: 1. 登陆微信公众平台登录授权,获取code; 2. 通过code获取access_token和openid; 3. 通过access_token和openid获取用户信息。 既然以上步骤大家已经心知肚明,下面我就重点说明一下第一个步骤在uniapp平台下的实现原理。 步骤一,在uniapp平台下实现微信授权登录功能 Uniapp平台下实现微信授权登录功能,需要通过uni-app-plugin-uni-wxapi插件进行实现。具体步骤如下: 1. 在HBuilderX中创建项目,然后在目录下跳转到dcloudio\uni-ui\uni-app-plugin-uni-wxapi插件下; 2. 打开公众号开发者平台,接着进入“基本配置”页面,将“OAuth2.0网页授权”启用。在可信域名中填写h5的域名; 3. 在插件下找到“common.js”和“instance.js”文件,修改获取微信的Appid和AppSecret,接着将文件进行增加至static文件夹下; 4. 在App.vue同级目录中,创建wxLogin.vue页面,用于页面的初始渲染。 事实上,Uni-app-plugin-uni-wxapi插件真的是非常好用的插件,只需要通过简单的设置,我们就可以轻松实现微信授权登录的功能。而本文,重点讲解的即为步骤一的功能实现原理。 实现微信授权登录功能,从而获取用户信息,是Uniapp开发小程序的核心功能。相较于其他开发语言,Uniapp不仅提供了完善的指引,而且还拥有庞大的社区,开发者可以在社区中学习交流,从而更好地解决问题,提升开发效率。因此,我们只需要需要在uniapp平台下正常操作,按照顺序完成上面的四个步骤,就能够实现微信授权登录功能获取用户信息,不再为此而烦恼。 ### 回答3: 在开始使用uniapp开发小程序前,你需要先注册微信小程序,并使用微信开发者工具创建一个小程序项目。当你完成这些步骤后,就可以开始使用uniapp来开发小程序了。为了实现获取微信授权登录功能,你需要遵循以下步骤。 1. 在小程序项目中创建一个按钮,当用户点击它时,触发获取微信授权登录的操作。 2. 在uniapp的公共方法中,定义一个获取微信授权登录的方法。 3. 在该方法中,使用uniapp的api向微信服务器发送请求,获取用户的授权信息。 4. 在接收到服务器返回的信息后,使用uniapp的方法将该信息保存在本地存储中。 5. 对于已经授权过的用户,可以通过读取本地存储中的信息来快速完成登录操作。 6. 对于未授权过的用户,需要在此时弹出微信授权登录窗口,提示用户进行授权。 7. 当用户完成授权后,我们可以通过微信提供的方法获取用户的基本信息。 8. 最后,我们需要将用户的基本信息和授权状态保存在数据库中,以便后续使用。 总之,uniapp提供了一些便捷的方法和api,可以很容易地实现获取微信授权登录功能。使用这些方法,你可以在小程序中创建出一个完整的、具有用户注册与登录功能的应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值