0
点赞
收藏
分享

微信扫一扫

uni-app 引入地图

<view class="page-body">
			<view class="page-section page-section-gap">
				<map  ref="map" id="map" @regionchange="regionchange" @markertap="clickdetail"
					style="width: 100%; height: 50vh;" :latitude="latitude" :longitude="longitude" :markers="covers">
				</map>
			</view>
		</view>

js部分

getLocation() {
				var _this = this;
				uni.getLocation({
					type: 'gcj02',

					success: function(res) {
						console.log(res, 'res')
						_this.longitude = res.longitude;
						_this.latitude = res.latitude;
						console.log(_this.longitude, _this.latitude)
						// _this.getworkerList();
					},
					fail(res) {
						console.log(res)
						//用户拒绝后引导用户开启定位
						_this.getSetting();
					}
				});
			},

getSetting() {

				uni.getSetting({
					success: res => {
						console.log('用户权限列表:', res.authSetting)
						if (res.authSetting['scope.userLocation']) {
							console.log('已授权userLocation')
							// 选择位置信息
							this.getLocation(); // 重新调取uni.getLocation
						} else {
							console.log('用户未授权userLocation')
							//2.用户第一次进来发起授权
							uni.showModal({
								title: '提示',
								content: '当前定位未开启,请点击确定手动开启定位',
								duration: 3000,
								success: (res) => {
									if (res.confirm) {
										this.openSetting() //点击确定引导客户开启定位
									} else if (res.cancel) {
										uni.showToast({
											title: '你拒绝了授权,无法获取设备定位信息',
											duration: 2000,
											icon: "none"
										});
									}
								}
							});
						}
					}
				})
			},
			openSetting() {
				uni.openSetting({
					success: (res) => {
						if (res.authSetting['scope.userLocation']) {
							// 5.用户在设置中点击了允许,调用选择位置信息函数
							this.getLocation() // 重新调取uni.getLocation
						} else {
							// 5.用户在设置中点击了不允许,展示拒绝授权信息
							uni.showToast({
								title: '你拒绝了授权,无法操作内容',
								icon: "none",
								duration: 3000,
							})
						}
					},
					fail: (err) => {
						console.log("打开设置失败", err)
					}
				})
			},

举报

相关推荐

0 条评论