uniapp开发微信小程序,经纬度获取城市

经纬度获取城市

			textinput(item) {
				console.log(item, 'item');
				if (item.value == '') {
					uni.chooseLocation({
						success: res => {
							const latitude = res.latitude
							const longitude = res.longitude
							console.log(res, '点击得');
							console.log('选择的经度:', longitude, '选择的纬度:', latitude)
							item.value = res.name
							item.lng = longitude
							item.lat = latitude
							console.log(item, 'item-item-item调起地图');

							const amapApiKey = '2a70d4e201b5a6b792237df017823f78';

						uni.request({
						    url: 'https://restapi.amap.com/v3/geocode/regeo',
						    data: {
						        key: amapApiKey,
						        location: longitude + ',' + latitude,
						        output: 'json'
						    },
						    success: res => {
						        console.log('逆地理编码服务返回的数据:', res.data);
						        // 根据返回的数据结构提取城市信息
						        const addressComponent = res.data.regeocode.addressComponent;
								console.log('匹配的城市:', addressComponent.city);
								console.log('匹配的省:', addressComponent.province);
								
						        if (addressComponent && addressComponent.city) {
									if(addressComponent.city.length==0){
										this.csname=addressComponent.province
										console.log('城市为空赋值省',this.csname);
									}else{
										this.csname=addressComponent.city
										console.log('城市不为空直接赋值',this.csname);
									}
									
									
						        } else {
						            console.log('无法从返回数据中获取城市信息');
						        }
						    },
						    fail: err => {
						        console.log('获取城市信息失败:', err);
						    }
						});
						},
						fail: err => {
							wx.showToast({
								title: '授权失败',
								icon: 'none',
								duration: 1000
							})
						}
					})
				} else {
					console.log('不为空,正常编辑');
				}
			},

城市获取经纬度

	bindCity(event) {
				console.log(event.name)
				
				const address = `${event.name}`;
				const key = 'c480611b1013378bc1145c5394b892c0'; // 替换为您的高德地图API密钥
				
				uni.request({
					url: `https://restapi.amap.com/v3/geocode/geo?key=${key}&address=${address}`,
					success: (res) => {
						console.log(res);
						if (res.data.status === '1' && res.data.count !== '0') {
							const location = res.data.geocodes[0].location;
							const [longitude, latitude] = location.split(',');
				
							console.log('经度:', longitude);
							console.log('纬度:', latitude);
							uni.setStorageSync('J',longitude)
							uni.setStorageSync('W',latitude)
							uni.setStorageSync('N',address)
							uni.navigateBack()
				
						} else {
							uni.showToast({
								title: '网络错误:获取经纬度失败',
								icon: 'none',
								duration: 2000
							});
						}
					},
					fail: (error) => {
						console.error(error);
					},
				});
			}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑云压城After

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值