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

uniapp實現(xiàn)微信小程序用戶實時位置定位并顯示地圖

這篇具有很好參考價值的文章主要介紹了uniapp實現(xiàn)微信小程序用戶實時位置定位并顯示地圖。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

目前,我們可以通過一些現(xiàn)成的api來實現(xiàn)此功能。下面我將介紹一下通過騰訊位置服務(wù)來實現(xiàn)此功能的具體操作流程。

1、在Hbuilder x中對項目進行權(quán)限開放

進入到manifest.json文件中

?

uniapp實現(xiàn)微信小程序用戶實時位置定位并顯示地圖

2、獲取調(diào)用騰訊位置服務(wù)所需的key

  1. 登錄騰訊地圖api: 騰訊位置服務(wù) - 立足生態(tài),連接未來
  2. 點擊控制臺--》應(yīng)用管理--》我的應(yīng)用--》添加Key---》填寫內(nèi)容--》添加

uniapp實現(xiàn)微信小程序用戶實時位置定位并顯示地圖

3、進入騰訊地圖文檔下載JavaScriptSDK ? ? [微信小程序JavaScript SDK | 騰訊位置服務(wù) (qq.com)](https://lbs.qq.com/miniProgram/jsSdk/jsSdkGuide/jsSdkOverview)

uniapp實現(xiàn)微信小程序用戶實時位置定位并顯示地圖

4、解壓剛下好的JavaScriptSDK

5、將min.js文件復(fù)制到項目static文件目錄下

?6、實現(xiàn)定位服務(wù)代碼

  • ?Vuex配置
// 引入騰訊地圖jssdk文件
import QQMapWX from "../static/js/qqmap-wx-jssdk.min.js"

export default {
	// 為當前模塊開啟命名空間
	namespaced: true,
	state: {
		// 默認城市
		city: '北京市',
        //當前位置的經(jīng)緯度
		x: 0,
		y: 0
	},
	mutations: {
		newCityFun(state, newCity) {
			state.city = newCity
			console.log(newCity)
			console.log(state.city)
		},
		getCity() {
			var that = this
			// 獲取用戶是否開啟 授權(quán)獲取當前的地理位置、速度的權(quán)限。
			uni.getSetting({
				success(res) {
					console.log(res)

					// 如果沒有授權(quán)
					if (!res.authSetting['scope.userLocation']) {
						// 則拉起授權(quán)窗口
						uni.authorize({
							scope: 'scope.userLocation',
							success() {
								let qqmapsdk = new QQMapWX({
									key: 'JOMBZ-MRF6U-GF2V3-BSKNG-757GO-O7FLR'
								});
								//點擊允許后--就一直會進入成功授權(quán)的回調(diào) 就可以使用獲取的方法了
								uni.getLocation({
									type: 'wgs84',
									success: function(res) {
										that.x = res.longitude
										that.y = res.latitude
										console.log("if", res)
										console.log('當前位置的經(jīng)度:' + res.longitude)
										console.log('當前位置的緯度:' + res.latitude)
										// 逆地址解析方法
										qqmapsdk.reverseGeocoder({
											location: {
												latitude: res.latitude,
												longitude: res.longitude
											},
											success(res) {
												var newCity = ''
												console.log(res)
												// 取到用戶的定位城市,賦值傳遞出去
												newCity = res.result
													.address_component.city
												// that.commit('m_location/newCityFun')
											}
										})
										uni.showToast({
											title: '當前位置的經(jīng)緯度:' + res.longitude +
												',' + res.latitude,
											icon: 'success',
											mask: true
										})
									},
									fail(error) {
										console.log('失敗', error)
									}
								})
							},
							fail(error) {
								//點擊了拒絕授權(quán)后--就一直會進入失敗回調(diào)函數(shù)--此時就可以在這里重新拉起授權(quán)窗口
								console.log('拒絕授權(quán)', error)

								uni.showModal({
									title: '提示',
									content: '若點擊不授權(quán),將無法使用位置功能',
									cancelText: '不授權(quán)',
									cancelColor: '#999',
									confirmText: '授權(quán)',
									confirmColor: '#f94218',
									success(res) {
										console.log(res)
										if (res.confirm) {
											// 選擇彈框內(nèi)授權(quán)
											uni.openSetting({
												success(res) {
													console.log(res.authSetting)
												}
											})
										} else if (res.cancel) {
											// 選擇彈框內(nèi) 不授權(quán)
											console.log('用戶點擊不授權(quán)')
										}
									}
								})
							}
						})
					} else {
						let qqmapsdk = new QQMapWX({
							key: 'JOMBZ-MRF6U-GF2V3-BSKNG-757GO-O7FLR'
						});
						// 有權(quán)限則直接獲取
						uni.getLocation({
							type: 'wgs84',
							success: function(res) {
								that.x = res.longitude
								that.y = res.latitude
								console.log("else", res)
								console.log('當前位置的經(jīng)度:' + res.longitude)
								console.log('當前位置的緯度:' + res.latitude)
								// 逆地址解析方法
								qqmapsdk.reverseGeocoder({
									location: {
										latitude: res.latitude,
										longitude: res.longitude
									},
									success(res) {
										var newCity = ''
										console.log(res)
										// 取到用戶的定位城市,賦值傳遞出去
										newCity = res.result.address_component.city
										// that.commit('m_location/newCityFun')
									},
									fail(res) {
										console.log("錯誤", res)
									}
								})
								//打開地圖
								uni.openLocation({
									latitude: that.y,
									longitude: that.x,
									success: function() {}
								});
								uni.showToast({
									title: '當前位置的經(jīng)緯度:' + res.longitude + ',' + res.latitude,
									icon: 'success',
									mask: true
								})
							},
							fail(error) {
								uni.showToast({
									title: '請勿頻繁調(diào)用!',
									icon: 'none',
								})
								console.log('失敗', error)
							}
						})
					}
				}
			})
		}
	},
	actions: {

	}
}

注意:需將以上代碼中的key,換成自己剛剛配置的key。文章來源地址http://www.zghlxwxcb.cn/news/detail-484610.html

let qqmapsdk = new QQMapWX({
	key: 'JOMBZ-MRF6U-GF2V3-BSKNG-757GO-O7FLR'
});
  • 頁面配置
<template>
		<view>{{addstr}}</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapGetters,
		mapActions
	} from 'vuex'
	export default {
		data() {
			return {
				title: 'Hello',
			}
		},
		onReady() {
			// 把m_location模塊中的 getCity 函數(shù)映射到當前組件
			this.getCity()
			
		},
		methods: {
			...mapMutations('m_location', ['getCity']),
		},
		computed: {
			// 從getter中引入addstr
			...mapGetters('m_location', ['addstr'])
		}
	}
</script>

到了這里,關(guān)于uniapp實現(xiàn)微信小程序用戶實時位置定位并顯示地圖的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

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

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

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

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包