赞
踩
经纬度获取城市
- 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);
- },
- });
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。