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

<style>
81 82 83 84
  .page-body-info {
    padding-bottom: 0;
    height: 440rpx;
  }
85 86 87 88 89
  .content{
    background:#FFFFFF;
    padding:40rpx;
    align-items: center;
  }
90 91
  .tips {
    font-size: 12px;
92
    margin: 15px 0;
93 94
    opacity: .8;
  }
95
</style>