wlzboy
2025-11-29 364adbc9a93a396b74e154f910c2a0a72bfb1a0f
app/pagesTask/components/DepartureSelector.vue
File was renamed from app/components/DepartureSelector.vue
@@ -26,7 +26,7 @@
        
        <view class="current-location-btn" @click="getCurrentLocation">
          <uni-icons type="location" size="20" color="#007AFF"></uni-icons>
          <text>当前位置</text>
        </view>
      </view>
      
@@ -199,48 +199,68 @@
      this.showAddressSuggestions = false
      this.addressSuggestions = []
    },
    // 获取当前位置
    getCurrentLocation() {
      uni.showLoading({
        title: '获取位置中...'
      })
      // 使用uni-app的GPS定位功能
      uni.getLocation({
        type: 'gcj02', // 返回国测局坐标,适用于国内地图
        success: (res) => {
          console.log('获取到GPS坐标:', res)
          const latitude = res.latitude
          const longitude = res.longitude
          // 更新GPS坐标
          this.$emit('update:longitude', longitude)
          this.$emit('update:latitude', latitude)
          // 调用逆地理编码接口,将坐标转换为地址
    geocoder(longitude, latitude){
 // 调用逆地理编码接口,将坐标转换为地址
          reverseGeocoder(latitude, longitude)
            .then(response => {
              uni.hideLoading()
              
              console.log('逆地理编码API完整响应:', JSON.stringify(response))
              if (response.code === 200 && response.data) {
                // 获取详细地址
                const address = response.data.address || response.data.formattedAddress || ''
                // 解析后端返回的数据(可能是字符串,需要parse)
                let responseData = response.data
                if (typeof responseData === 'string') {
                  try {
                    responseData = JSON.parse(responseData)
                  } catch (e) {
                    console.error('解析响应数据失败:', e)
                    responseData = {}
                  }
                }
                
                // 更新地址
                this.$emit('update:address', address)
                console.log('解析后的responseData:', responseData)
                
                // 触发位置获取成功事件
                this.$emit('location-success', {
                  address: address,
                  longitude: longitude,
                  latitude: latitude
                })
                // 腾讯地图API返回格式: {status: 0, result: {address: "..."}}
                let address = ''
                if (responseData.status === 0 && responseData.result) {
                  address = responseData.result.address || responseData.result.formatted_addresses?.recommend || ''
                } else if (responseData.address) {
                  // 兼容其他可能的返回格式
                  address = responseData.address
                } else if (responseData.formattedAddress) {
                  address = responseData.formattedAddress
                }
                
                console.log('逆地理编码成功:', address)
                this.$modal.showToast('已获取当前位置')
                console.log('解析出的地址:', address)
                if (address) {
                  // 更新地址
                  this.$emit('update:address', address)
                  // 触发位置获取成功事件
                  this.$emit('location-success', {
                    address: address,
                    longitude: longitude,
                    latitude: latitude
                  })
                  console.log('逆地理编码成功,地址已更新:', address)
                  this.$modal.showToast('已获取当前位置')
                } else {
                  console.error('未能从响应中提取地址,responseData:', responseData)
                  // 即使地址解析失败,坐标已保存,触发事件
                  this.$emit('location-success', {
                    address: '',
                    longitude: longitude,
                    latitude: latitude
                  })
                  this.$modal.showToast('位置解析失败,请手动输入地址')
                }
              } else {
                console.error('逆地理编码失败:', response.msg)
                console.error('逆地理编码失败,response.code:', response.code, 'msg:', response.msg)
                
                // 即使地址解析失败,坐标已保存,触发事件
                this.$emit('location-success', {
@@ -265,11 +285,41 @@
              
              this.$modal.showToast('位置解析失败,但GPS坐标已保存')
            })
    },
    // 获取当前位置
    getCurrentLocation() {
      uni.showLoading({
        title: '获取位置中...'
      })
      // 使用uni-app的GPS定位功能
      uni.getLocation({
        type: 'gcj02', // 返回国测局坐标,适用于国内地图
        success: (res) => {
          console.log('获取到GPS坐标:', res)
          const latitude = res.latitude
          const longitude = res.longitude
          // 更新GPS坐标
          this.$emit('update:longitude', longitude)
          this.$emit('update:latitude', latitude)
          this.geocoder(longitude, latitude)
        },
        fail: (err) => {
          uni.hideLoading()
          console.error('获取位置失败:', err)
          //我们使用默认的坐标来处理
          //23.20593,113.228998
          const longitude = 113.228998
          const latitude = 23.20593
          this.$emit('location-success', {
            address: '',
            longitude: longitude,
            latitude: latitude
          })
          this.geocoder(longitude, latitude)
          // 提示用户可能的原因
          let errorMsg = '获取位置失败'
          if (err.errMsg && err.errMsg.includes('auth deny')) {
@@ -373,7 +423,7 @@
        background-color: #f0f7ff;
        border-radius: 10rpx;
        white-space: nowrap;
        min-height: 70rpx;
        min-height: 40rpx;
        
        &:active {
          background-color: #e0f0ff;