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

<style>
78 79 80 81 82 83 84 85 86 87
  .page-body-info {
    padding-bottom: 0;
    height: 440rpx;
  }

  .tips {
    font-size: 12px;
    margin-top: 15px;
    opacity: .8;
  }
88
</style>