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

<style>
  .page-body-info {
    padding-bottom: 0;
    height: 440rpx;
  }

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