uniapp development applet - map selection location (error: wx.chooseLocation need to be declared in the requiredPrivateInfos field in )

1. Problems

When clicking on the location, an error message is reported:wx.chooseLocation need to be declared in the requiredPrivateInfos field in app.json/ext.json

insert image description here

Two, the solution

2.1 Step 1:
Open manifest.jsonthe source code view, find the mp-weixin node, and add the requiredPrivateInfos configuration. The source code is as follows:

"requiredPrivateInfos": ["getLocation", "chooseLocation"],  //使用地图

insert image description here
2.2 The second step:
Search the WeChat public platform After entering, click the development management under the development , click the interface setting , and open the API used, as shown in the figure below:
insert image description here

3. Specific effect and specific code

3.1 Effect
insert image description here

3.2 Code

<template>
	<view class="">
		<view class="dingwei"  @click="getMapLocation()">
			<image src="../../static/images/icon_dw@2x(1).png"></image>定位
		</view>
	</view>
</template>

<script>
	export default {
      
      
		data() {
      
      
			return {
      
      
				addressData: {
      
      
					longitude: '118.06637', //经度
					latitude: '37.665928', //纬度
					details: '', //详细地址
				},
			}
		},
		
	
		methods: {
      
      
			//定位选择详细地址
			getMapLocation() {
      
      
				uni.chooseLocation({
      
      
					success: res => {
      
      
						console.log(res);
						this.addressData.details = res.name;
						this.addressData.longitude = res.longitude;
						this.addressData.latitude = res.latitude;
					},
					fail: () => {
      
      
						// 如果用uni.chooseLocation没有获取到地理位置,则需要获取当前的授权信息,判断是否有地理授权信息
						uni.getSetting({
      
      
							success: res => {
      
      
								console.log(res);
								var status = res.authSetting;
								if (!status['scope.userLocation']) {
      
      
									// 如果授权信息中没有地理位置的授权,则需要弹窗提示用户需要授权地理信息
									uni.showModal({
      
      
										title: '是否授权当前位置',
										content: '需要获取您的地理位置,请确认授权,否则地图功能将无法使用',
										success: tip => {
      
      
											if (tip.confirm) {
      
      
												// 如果用户同意授权地理信息,则打开授权设置页面,判断用户的操作
												uni.openSetting({
      
      
													success: data => {
      
      
														// 如果用户授权了地理信息在,则提示授权成功
														if (data.authSetting['scope.userLocation'] === true) {
      
      
															uni.showToast({
      
      
																title: '授权成功',
																icon: 'success',
																duration: 1000
															});
															// 授权成功后,然后再次chooseLocation获取信息
															uni.chooseLocation({
      
      
																success: res => {
      
      
																	console.log('详细地址',res);
																	this.addressData.details =res.name;
																	this.addressData.longitude =res.longitude;
																	this.addressData.latitude =res.latitude;
																}
															});
														} else {
      
      
															uni.showToast({
      
      
																title: '授权失败',
																icon: 'none',
																duration: 1000
															});
														}
													}
												});
											}
										}
									});
								}
							},
							fail: res => {
      
      
								uni.showToast({
      
      
									title: '调用授权窗口失败',
									icon: 'none',
									duration: 1000
								});
							}
						});
					}
				});
			},

		}
	}
</script>

That's it! ! !

Guess you like

Origin blog.csdn.net/weixin_48596030/article/details/131471498