choose-location.uvue 3.0 KB
Newer Older
1
<template>
2 3 4
  <view>
    <page-head :title="title"></page-head>
    <view class="uni-padding-wrap">
5 6 7 8
      <view class="uni-column content">
        <text class="uni-hello-text">位置信息</text>
        <text v-if="!hasLocation" class="uni-title-text uni-common-mt">未选择位置</text>
        <view v-if="hasLocation" style="align-items: center;">
VK1688's avatar
VK1688 已提交
9
          <text class="uni-common-mt">{{locationName}}</text>
10 11
          <text class="uni-common-mt">{{locationAddress}}</text>
          <view class="uni-common-mt" v-if="location.latitude.length>1">
12 13 14
            <text>E: {{location.longitude[0]}}°{{location.longitude[1]}}′</text>
            <text>\nN: {{location.latitude[0]}}°{{location.latitude[1]}}′</text>
          </view>
15
        </view>
16 17
      </view>
      <view class="uni-btn-v">
18
        <text class="tips">注意:Web和App需要正确配置地图服务商的Key并且保证Key的权限和余额足够,才能正常选择位置</text>
VK1688's avatar
VK1688 已提交
19 20
        <button class="btn" type="primary" @tap="chooseLocation">选择位置</button>
        <button class="btn" @tap="clear">清空</button>
21 22 23
      </view>
    </view>
  </view>
24 25
</template>
<script lang="uts">
26 27 28
  type Location = {
    latitude: string[]
    longitude: string[]
29 30
  }
  export default {
DCloud-WZF's avatar
DCloud-WZF 已提交
31
    data() {
32 33 34
      return {
        title: 'chooseLocation',
        hasLocation: false,
35 36 37 38
        location: {
          latitude: [],
          longitude: []
        } as Location,
VK1688's avatar
VK1688 已提交
39
        locationName: '',
40 41
        locationAddress: '',
        dialogPagesNum: -1
42 43 44 45 46 47
      }
    },
    methods: {
      chooseLocation: function () {
        uni.chooseLocation({
          success: (res) => {
48
            console.log('chooseLocation success', res)
49
            this.hasLocation = true
50
            this.location = this.formatLocation(res.longitude, res.latitude)
VK1688's avatar
VK1688 已提交
51
            this.locationName = res.name
52 53 54
            this.locationAddress = res.address
          }
        })
55 56 57 58 59 60
        setTimeout(() => {
          const pages = getCurrentPages()
          const page = pages[pages.length - 1]
          const dialogPages = page.getDialogPages()
          this.dialogPagesNum = dialogPages.length
        }, 500)
61
      },
62 63 64 65 66 67 68 69 70 71 72 73 74 75
      formatLocation: function(longitude:number, latitude:number):Location {
        const longitudeArr = longitude.toString().split('.')
        const latitudeArr = latitude.toString().split('.')
        if(longitudeArr.length>1){
          longitudeArr[1] = longitudeArr[1].substring(0,2)
        }
        if(latitudeArr.length>1){
          latitudeArr[1] = latitudeArr[1].substring(0,2)
        }
        return {
          longitude: longitudeArr,
          latitude: latitudeArr
        }
      },
76 77 78 79 80
      clear: function () {
        this.hasLocation = false
      }
    }
  }
81 82 83
</script>

<style>
84 85 86 87
  .page-body-info {
    padding-bottom: 0;
    height: 440rpx;
  }
88 89 90 91 92
  .content{
    background:#FFFFFF;
    padding:40rpx;
    align-items: center;
  }
93 94
  .tips {
    font-size: 12px;
95
    margin: 15px 0;
96 97
    opacity: .8;
  }
VK1688's avatar
VK1688 已提交
98 99 100
  .btn {
    margin-top: 10px;
  }
101
</style>