国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

uniapp實現(xiàn)城市列表選擇獲取經(jīng)緯度、附帶搜索功能(移動端、微信小程序)

這篇具有很好參考價值的文章主要介紹了uniapp實現(xiàn)城市列表選擇獲取經(jīng)緯度、附帶搜索功能(移動端、微信小程序)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

準備工作

所用到的技術

  • 騰訊地圖微信小程序SDK
  • 高德地圖WebServiceAPI服務
  • APP獲取是否授權(quán)插件
  • uview框架(不是必須)

接下來帶大家去申請

騰訊地圖微信小程序SDK

微信小程序JavaScript SDK
點擊下載 JavaScriptSDK v1.2
然后去申請騰訊地圖的 key

先創(chuàng)建應用 > 在添加key
uniapp列表選擇,UNIAPP,微信小程序,前端,微信小程序,前端框架,web app
uniapp列表選擇,UNIAPP,微信小程序,前端,微信小程序,前端框架,web app

uniapp列表選擇,UNIAPP,微信小程序,前端,微信小程序,前端框架,web app
在應用列表中就能看到我們申請的 key
uniapp列表選擇,UNIAPP,微信小程序,前端,微信小程序,前端框架,web app

高德地圖WebServiceAPI服務

高德地圖開放平臺
高德地圖這里只需要去申請一個 WebServiceAPI 服務的key
申請流程和騰訊地圖一樣
先創(chuàng)建應用 > 在添加key
uniapp列表選擇,UNIAPP,微信小程序,前端,微信小程序,前端框架,web app
uniapp列表選擇,UNIAPP,微信小程序,前端,微信小程序,前端框架,web app

APP獲取是否授權(quán)插件

dcloud 插件市場添加 插件鏈接
直接使用 HBuilderX 導入到項目
選擇自己的項目即可
uniapp列表選擇,UNIAPP,微信小程序,前端,微信小程序,前端框架,web app
uniapp列表選擇,UNIAPP,微信小程序,前端,微信小程序,前端框架,web app

uview框架

uview 框架這里就不寫操作步驟了
大家根據(jù)步驟安裝就行了

代碼實現(xiàn)

兩個頁面 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: '',// 區(qū)、縣
					lng: '',// 經(jīng)度
					lat: '' // 緯度
				}
			}
		},
		onLoad() {
			// 位置授權(quán)
			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() {
						// 首次拒絕授權(quán)位置(根據(jù)自己要求配置或不配默認地址)
						_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();
						},
					});
				});
			},
			// 騰訊地圖經(jīng)緯度轉(zhuǎn)中文地址
			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>
		<!-- 搜索結(jié)果模塊 -->
		<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>
		<!-- 城市、區(qū)縣切換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>
			<!-- 右側(cè)索引 -->
			<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>
		<!-- 區(qū)縣展示 -->
		<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' // 結(jié)尾有下載地址
	import { area } from './area.js' // 結(jié)尾有下載地址
	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: '國內(nèi)'}, {name: ''}],
				areaList: [],
				areaAll: [],
				searchList: [],
				current: 0,
				area: {
					city: '',
					area: ''
				},
				isFocus: false,
				isCancel: false
			}
		},
		onLoad(options) {
			// 首頁傳過來的數(shù)據(jù)
			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
					}
				})
			})
			// 根據(jù)城市ID查找出改城市所屬的區(qū)縣
			this.areaList = area[this.address.cityId]
			this.area.city = item.city
			this.area.area = item.area
			// 初始tabs標題
			this.tabsList[1].name = name + '區(qū)縣'
		},
		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") // 安卓方法返回用戶是否授權(quán)獲取位置
			} else if (device === 'ios') {
				isLocations = permision.judgeIosPermission("location") // ios方法返回用戶是否授權(quán)獲取位置
			} else {
				isLocations = await this.weChatGetLocation() // 返回微信小程序是否授權(quán)了位置
			}
			// 未授權(quán)時彈窗提示用戶授權(quán)
			if (!isLocations) {
				uni.showModal({
					title: '提示',
					content: '請開啟位置信息權(quán)限',
					showCancel: false,
					success() {
						// 設備為android或ios是跳轉(zhuǎn)系統(tǒng)設置(用戶自行設置)
						if (device === 'android' || device === 'ios') {
							permision.gotoAppPermissionSetting(); // 打開權(quán)限設置界面
							uni.navigateBack()
						} else {
							// 微信小程序時打開小程序設置界面
							uni.openSetting({
								success: (res) => {}
							})
						}
					}
				});
			}
		},
		mounted() {
			// 初始化城市JS數(shù)據(jù)
			this.cityList.forEach(item => {
				this.cityAll = this.cityAll.concat(item.city)
			})
		},
		onUnload() {
			// 銷毀事件監(jiān)聽
			uni.$off('againLocation')
			uni.$off('getLocation')
		},
		methods: {
			// 重新定位
			Repositioning() {
				uni.$emit('againLocation')
				uni.navigateBack()
			},
			// 獲取定位
			getLocation(item) {
				let name = item
				uni.$emit('getLocation', name)
				uni.navigateBack()
			},
			// 區(qū)定位
			areaLocation(item) {
				uni.$emit('getLocation', this.area.city, item)
				uni.navigateBack()
			},
			// 右邊索引點擊
			letterTap(index) {
				this.scrollId = this.letter[index]
			},
			// tabs狀態(tài)切換
			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
				}
			},
			// 微信小程序是否授權(quán)位置信息
			weChatGetLocation() {
				return new Promise((resolve, reject) => {
					uni.authorize({
						scope: 'scope.userLocation',
						success() {
							resolve(true)
							console.log('授權(quán)了')
						},
						fail() {
							resolve(false)
							console.log('沒有授權(quán)')
						}
					})
				})
			}
		}
	}
</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時在開發(fā)階段需要打開 定位功能
在項目的 manifest.json 中的 APP模塊配置
uniapp列表選擇,UNIAPP,微信小程序,前端,微信小程序,前端框架,web app
有問題可以留言也可以私我。
到這里也就結(jié)束了,謝謝大家觀看。

城市、區(qū)縣數(shù)據(jù)文章來源地址http://www.zghlxwxcb.cn/news/detail-676913.html

到了這里,關于uniapp實現(xiàn)城市列表選擇獲取經(jīng)緯度、附帶搜索功能(移動端、微信小程序)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • vue 高德地圖 —— 點標記、信息彈窗、網(wǎng)頁導航、獲取經(jīng)緯度及當前城市信息

    新建 components/amap.vue 文件,封裝高德地圖組件: 接下來,在需要使用的組件中引入 amap.vue :

    2023年04月15日
    瀏覽(27)
  • 用戶Ip地址和百度地圖api接口獲取用戶地理位置(經(jīng)緯度坐標,城市)

    ?php //獲取用戶ip(外網(wǎng)ip 服務器上可以獲取用戶外網(wǎng)Ip 本機ip地址只能獲取127.0.0.1) function ? getip(){ ???? if (! empty ( $_SERVER [ \\\"HTTP_CLIENT_IP\\\" ])){ ???? $cip ? =? $_SERVER [ \\\"HTTP_CLIENT_IP\\\" ]; ???? } ???? else ? if (! empty ( $_SERVER [ \\\"HTTP_X_FORWARDED_FOR\\\" ])){ ???? $cip ? =? $_SERVER [ \\\"HTTP_X_FOR

    2024年02月11日
    瀏覽(35)
  • uniapp 在app中獲取經(jīng)緯度

    uniapp 在app中獲取經(jīng)緯度

    在uniapp中app端,uni.getLocation獲取經(jīng)緯度會有大概1-2公里的偏差,在實際項目中,有的需求對經(jīng)緯度的準確度要求比較嚴格,研究了很多種方式,最終發(fā)現(xiàn)使用高德地圖api的微信小程序的插件獲取的準確性是最準的,偏差最小的。 1.先去高德地圖獲取key,注意,這里是要獲取微

    2024年02月15日
    瀏覽(103)
  • uniapp通過ip獲取其地址、經(jīng)緯度、詳細地址:

    uniapp通過ip獲取其地址、經(jīng)緯度、詳細地址:

    1.方法: 查看ip內(nèi)容:http://pv.sohu.com/cityjson?ie=utf-8 【1】js獲取ip地址: 【2】uni-app獲取ip地址:(此方法會跨域報錯=后續(xù)找到解決方法再補充) 【3】使用H5自帶的獲取位置 【4】使用百度地圖獲取位置 【5】微信js-sdk自帶的API 2.案例: 3.最終效果:

    2024年02月12日
    瀏覽(21)
  • uniapp微信小程序getLocation獲取經(jīng)緯度報錯

    uniapp微信小程序getLocation獲取經(jīng)緯度報錯

    uniapp開發(fā)微信小程序時,需要做一個授權(quán)位置信息的需求,使用getLocation獲取用戶當前的經(jīng)緯度。期間遇到了一個問題老是報這個錯誤:“getLocation:fail the api need to be declared in the requiredPrivateInfos field in app.json/ext.json” 根據(jù)官方文檔 https://developers.weixin.qq.com/miniprogram/dev/api/loc

    2024年02月13日
    瀏覽(83)
  • uniapp 開發(fā)微信小程序,獲取經(jīng)緯度轉(zhuǎn)化詳細地址

    uniapp 開發(fā)微信小程序,獲取經(jīng)緯度轉(zhuǎn)化詳細地址

    正常開發(fā)中,我們通過 uni.getLocation 只能得到經(jīng)緯度,微信又沒有給我們具體的地理位置,這個時候我們可以通過反編譯,來獲取詳細地址。操作如下 第一步:我們先去騰訊地圖申請key騰訊地圖? 在 控制臺== 應用管理 == 我的應用 ==創(chuàng)建應用 == 添加key== 除了必填的,勾選Web

    2024年02月04日
    瀏覽(93)
  • uniapp開發(fā)小程序解析經(jīng)緯度獲取當前位置信息(騰訊地圖二)

    uniapp開發(fā)小程序解析經(jīng)緯度獲取當前位置信息(騰訊地圖二)

    選擇了騰訊地圖定位 騰訊地圖官網(wǎng) 具體實踐步驟如下: 申請開發(fā)者密鑰 申請密鑰key 開通webserviceAPI服務 下載小程序SDK 騰訊地圖小程序文檔sdk 微信后臺配置請求request域名 小程序管理后臺 詳細步驟 1. 下載解壓后的 qqmap-wx-jssdk.js文件放到項目中,然后在頁面引入使用 [ uni-app中

    2024年02月15日
    瀏覽(374)
  • uniapp開發(fā)小程序解析經(jīng)緯度獲取當前位置信息(高德地圖三)

    uniapp開發(fā)小程序解析經(jīng)緯度獲取當前位置信息(高德地圖三)

    選擇了高德地圖定位 高德地圖官網(wǎng) 小程序步驟如下: ? ? ?1.首先創(chuàng)建應用 ? ? ??2.點擊增添key按鈕申請小程序key ? ? ? ? 3.然后下載它的微信小程序版 SDK:微信小程序 SDK ? ? ? ? 4.把下載的sdk放在公共的文件里,這里我放在了utils文件目錄下 ? ? ? ? ?5.使用頁面導入此

    2024年02月02日
    瀏覽(101)
  • uniapp---- 獲取當前位置的經(jīng)緯度等信息的詳細步驟(包含小程序)

    uniapp---- 獲取當前位置的經(jīng)緯度等信息的詳細步驟(包含小程序)

    1.在項目中進行配置,我選擇的是高德地圖,填寫相關信息。 2.進入高德官網(wǎng):https://lbs.amap.com/upgrade#quota,進行登錄注冊,進入到“控制臺”。 3.打開 “應用管理” - “我的應用”頁面,點擊“創(chuàng)建新應用”,根據(jù)頁面提示填寫內(nèi)容創(chuàng)建應用。 4.在應用下點擊“添加”為應用

    2024年02月11日
    瀏覽(109)
  • uniapp使用高德地圖地理位置逆解析/將獲取到的經(jīng)緯度轉(zhuǎn)化為地址

    uniapp使用高德地圖地理位置逆解析/將獲取到的經(jīng)緯度轉(zhuǎn)化為地址

    1、在高德登錄注冊,進行個人或企業(yè)開發(fā)者認證(個人開發(fā)者可以隨時升級企業(yè)開發(fā)者) 高德地圖開發(fā)aip網(wǎng)址 2、進入控制臺,按以下圖示操作 添加完成之后把key復制一下。 3、使用uni.request進行請求(用什么框架就這么請求,這里示例的是uniapp),請求地址為:https://rest

    2024年02月08日
    瀏覽(115)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包