choose-location.uvue 5.2 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
        <!-- #ifdef APP-IOS -->
        <button class="btn" type="primary" @tap="chooseLocationByPlugin">通过 uts 插件调用 chooseLocation</button>
        <!-- #endif -->
27 28 29 30 31
      </view>
    </view>
  </view>
</template>
<script lang="uts">
32 33 34 35 36
  import {
    state,
    setLifeCycleNum
  } from '@/store/index.uts'

37 38 39 40 41 42 43 44 45 46 47 48 49
  type Location = {
    latitude: string[]
    longitude: string[]
  }
  export default {
    data() {
      return {
        title: 'chooseLocation',
        hasLocation: false,
        location: {
          latitude: [],
          longitude: []
        } as Location,
VK1688's avatar
VK1688 已提交
50
        locationName: '',
51
        locationAddress: '',
VK1688's avatar
VK1688 已提交
52 53 54 55
        dialogPagesNum: -1,
        hoverLocation: false,
        hoverKeyword: false,
        hoverPayload: false
56 57
      }
    },
58 59 60 61 62
    onShow() {
      console.log("Page Show");
      // 自动化测试
      setLifeCycleNum(state.lifeCycleNum + 1)
    },
63 64
    onHide() {
      console.log("Page Hide");
65 66
      // 自动化测试
      setLifeCycleNum(state.lifeCycleNum - 1)
67
    },
68 69
    methods: {
      chooseLocation: function () {
VK1688's avatar
VK1688 已提交
70
        let chooseLocationOptions = {
71 72 73 74
          success: (res) => {
            console.log('chooseLocation success', res)
            this.hasLocation = true
            this.location = this.formatLocation(res.longitude, res.latitude)
VK1688's avatar
VK1688 已提交
75
            this.locationName = res.name
76 77
            this.locationAddress = res.address
          }
VK1688's avatar
VK1688 已提交
78
        } as ChooseLocationOptions
VK1688's avatar
VK1688 已提交
79
        if (this.hoverLocation) {
VK1688's avatar
VK1688 已提交
80 81
          chooseLocationOptions.latitude = 39.908823
          chooseLocationOptions.longitude = 116.39747
VK1688's avatar
VK1688 已提交
82 83
        }
        if (this.hoverKeyword) {
VK1688's avatar
VK1688 已提交
84
          chooseLocationOptions.keyword = '公园'
VK1688's avatar
VK1688 已提交
85 86
        }
        if (this.hoverPayload) {
VK1688's avatar
VK1688 已提交
87
          chooseLocationOptions.payload = {
VK1688's avatar
VK1688 已提交
88 89 90 91
            token: 'xxx'
          }
        }
        uni.chooseLocation(chooseLocationOptions)
92
        // 自动化测试
93
        setTimeout(() => {
VK1688's avatar
VK1688 已提交
94
          this.test()
95
        }, 500)
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
      },
      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 已提交
113 114 115 116 117 118 119 120 121 122
      },
      changeLocationBoolean(checked : boolean) {
        this.hoverLocation = checked
      },
      changeKeywordBoolean(checked : boolean) {
        this.hoverKeyword = checked
      },
      changePayloadBoolean(checked : boolean) {
        this.hoverPayload = checked
      },
123 124 125 126 127
      // #ifdef APP-IOS
      chooseLocationByPlugin(){
        uni.chooseLocationPlugin()
      },
      // #endif
VK1688's avatar
VK1688 已提交
128 129 130 131
      // 自动化测试
      test() {
        const pages = getCurrentPages()
        const page = pages[pages.length - 1]
132
        // #ifdef APP || WEB
VK1688's avatar
VK1688 已提交
133 134
        const dialogPages = page.getDialogPages()
        this.dialogPagesNum = dialogPages.length
135
        // #endif
136 137 138 139 140 141 142 143 144
      },
      // 自动化测试
      setLifeCycleNum(value : number) {
        setLifeCycleNum(value)
      },
      // 自动化测试
      getLifeCycleNum() : number {
        return state.lifeCycleNum
      },
145 146 147 148 149 150 151 152 153
    }
  }
</script>

<style>
  .page-body-info {
    padding-bottom: 0;
    height: 440rpx;
  }
154 155 156 157 158
  .content{
    background:#FFFFFF;
    padding:40rpx;
    align-items: center;
  }
159 160
  .tips {
    font-size: 12px;
161
    margin: 15px 0;
162 163
    opacity: .8;
  }
VK1688's avatar
VK1688 已提交
164 165 166
  .btn {
    margin-top: 10px;
  }
167
</style>