choose-location.uvue 4.4 KB
Newer Older
1 2 3 4
<template>
  <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 15 16 17
            <text>E: {{location.longitude[0]}}°{{location.longitude[1]}}′</text>
            <text>\nN: {{location.latitude[0]}}°{{location.latitude[1]}}′</text>
          </view>
        </view>
      </view>
      <view class="uni-btn-v">
VK1688's avatar
VK1688 已提交
18 19 20 21
        <text class="tips">注意:\n1. Web和App需要正确配置地图服务商的Key并且保证Key的权限和余额足够,才能正常选择位置\n2. 若没有关联uniCloud空间,则只能全屏地图选点,不能根据POI选择位置\n3. payload参数会原样透传给uni-map-co,可用于用户鉴权</text>
        <boolean-data :defaultValue="false" title="是否指定位置为天安门" @change="changeLocationBoolean"></boolean-data>
        <boolean-data :defaultValue="false" title="是否携带keyword参数" @change="changeKeywordBoolean"></boolean-data>
        <boolean-data :defaultValue="false" title="是否携带payload参数" @change="changePayloadBoolean"></boolean-data>
VK1688's avatar
VK1688 已提交
22 23
        <button class="btn" type="primary" @tap="chooseLocation">选择位置</button>
        <button class="btn" @tap="clear">清空</button>
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
      </view>
    </view>
  </view>
</template>
<script lang="uts">
  type Location = {
    latitude: string[]
    longitude: string[]
  }
  export default {
    data() {
      return {
        title: 'chooseLocation',
        hasLocation: false,
        location: {
          latitude: [],
          longitude: []
        } as Location,
VK1688's avatar
VK1688 已提交
42
        locationName: '',
43
        locationAddress: '',
VK1688's avatar
VK1688 已提交
44 45 46 47
        dialogPagesNum: -1,
        hoverLocation: false,
        hoverKeyword: false,
        hoverPayload: false
48 49 50 51
      }
    },
    methods: {
      chooseLocation: function () {
VK1688's avatar
VK1688 已提交
52
        let chooseLocationOptions = {
53 54 55 56
          success: (res) => {
            console.log('chooseLocation success', res)
            this.hasLocation = true
            this.location = this.formatLocation(res.longitude, res.latitude)
VK1688's avatar
VK1688 已提交
57
            this.locationName = res.name
58 59
            this.locationAddress = res.address
          }
VK1688's avatar
VK1688 已提交
60
        } as ChooseLocationOptions
VK1688's avatar
VK1688 已提交
61
        if (this.hoverLocation) {
VK1688's avatar
VK1688 已提交
62 63
          chooseLocationOptions.latitude = 39.908823
          chooseLocationOptions.longitude = 116.39747
VK1688's avatar
VK1688 已提交
64 65
        }
        if (this.hoverKeyword) {
VK1688's avatar
VK1688 已提交
66
          chooseLocationOptions.keyword = '公园'
VK1688's avatar
VK1688 已提交
67 68
        }
        if (this.hoverPayload) {
VK1688's avatar
VK1688 已提交
69
          chooseLocationOptions.payload = {
VK1688's avatar
VK1688 已提交
70 71 72 73
            token: 'xxx'
          }
        }
        uni.chooseLocation(chooseLocationOptions)
74
        // 自动化测试
75
        setTimeout(() => {
VK1688's avatar
VK1688 已提交
76
          this.test()
77
        }, 500)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
      },
      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
        }
      },
      clear: function () {
        this.hasLocation = false
VK1688's avatar
VK1688 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
      },
      changeLocationBoolean(checked : boolean) {
        this.hoverLocation = checked
      },
      changeKeywordBoolean(checked : boolean) {
        this.hoverKeyword = checked
      },
      changePayloadBoolean(checked : boolean) {
        this.hoverPayload = checked
      },
      // 自动化测试
      test() {
        const pages = getCurrentPages()
        const page = pages[pages.length - 1]
        const dialogPages = page.getDialogPages()
        this.dialogPagesNum = dialogPages.length
111 112 113 114 115 116 117 118 119 120
      }
    }
  }
</script>

<style>
  .page-body-info {
    padding-bottom: 0;
    height: 440rpx;
  }
121 122 123 124 125
  .content{
    background:#FFFFFF;
    padding:40rpx;
    align-items: center;
  }
126 127
  .tips {
    font-size: 12px;
128
    margin: 15px 0;
129 130
    opacity: .8;
  }
VK1688's avatar
VK1688 已提交
131 132 133
  .btn {
    margin-top: 10px;
  }
134
</style>