get-location.uvue 3.6 KB
Newer Older
杜庆泉's avatar
杜庆泉 已提交
1
<template>
杜庆泉's avatar
杜庆泉 已提交
2 3
  <view>
    <page-head :title="title"></page-head>
杜庆泉's avatar
杜庆泉 已提交
4 5 6 7 8
    <view class="uni-hello-text">
    	<text class="hello-text">当前示例基于操作系统自身定位能力,暂不支持gcj标准,逆地理信息等功能,如果需要类似能力可以使用腾讯定位等三方定位服务,参考示例:</text>
    	<u-link :href="'https://gitcode.net/dcloud/uni-api/'" :text="'https://gitcode.net/dcloud/uni-api/'" :inWhiteList="true"></u-link>
    </view>

杜庆泉's avatar
杜庆泉 已提交
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
    <view class="uni-padding-wrap uni-common-mt">
      <view class="uni-list">
        <radio-group @change="radioChange">
          <radio class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in items" :key="item.value"
            :class="index < items.length - 1 ? 'uni-list-cell-line': ''" :value="item.value"
            :checked="index === current">
            {{item.name}}
          </radio>
        </radio-group>
      </view>
      <view class="uni-list-cell uni-list-cell-pd">
        <view class="uni-list-cell-db">高度信息</view>
        <switch :checked="altitudeSelect" @change="altitudeChange" />
      </view>
      <view class="uni-list-cell uni-list-cell-pd">
        <view class="uni-list-cell-db">开启高精度定位</view>
        <switch :checked="isHighAccuracySelect" @change="highAccuracySelectChange" />
      </view>
      <view class="uni-list-cell uni-list-cell-pd">
        <view class="uni-list-cell-db">是否解析地址信息</view>
        <switch :checked="geocodeSelect" @change="geocodeChange" />
      </view>
      <text>{{exeRet}}</text>
      <view class="uni-btn-v">
        <button class="uni-btn" type="default" @tap="getLocationTap">获取定位</button>
      </view>
杜庆泉's avatar
杜庆泉 已提交
35
    </view>
杜庆泉's avatar
杜庆泉 已提交
36
  </view>
杜庆泉's avatar
杜庆泉 已提交
37
</template>
Y
yurj26 已提交
38
<script lang="uts">
杜庆泉's avatar
杜庆泉 已提交
39 40 41 42 43 44 45 46 47 48 49 50
  type ItemType = {
    value : string,
    name : string,
  }
  export default {
    data() {
      return {
        title: 'get-location',
        altitudeSelect: false,
        isHighAccuracySelect: false,
        geocodeSelect: false,
        exeRet: '',
杜庆泉's avatar
杜庆泉 已提交
51
        items: [
杜庆泉's avatar
杜庆泉 已提交
52 53 54
        {
          value: 'wgs84',
          name: 'wgs84'
杜庆泉's avatar
杜庆泉 已提交
55 56 57 58
        },
        {
          value: 'gcj02',
          name: 'gcj02'
杜庆泉's avatar
杜庆泉 已提交
59
        }
杜庆泉's avatar
杜庆泉 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        ] as ItemType[],
        current: 0,
      }
    },
    methods: {
      altitudeChange: function (e : SwitchChangeEvent) {
        this.altitudeSelect = e.detail.value
      },
      geocodeChange: function (e : SwitchChangeEvent) {
        this.geocodeSelect = e.detail.value
      },
      highAccuracySelectChange: function (e : SwitchChangeEvent) {
        this.isHighAccuracySelect = e.detail.value
      },
      radioChange(e : RadioGroupChangeEvent) {
        for (let i = 0; i < this.items.length; i++) {
          if (this.items[i].value === e.detail.value) {
            this.current = i;
            break;
          }
        }
      },
      getLocationTap: function () {
        uni.showLoading({
          title: '定位中'
        })
        uni.getLocation(({
          type: this.items[this.current].value,
          altitude: this.altitudeSelect,
          isHighAccuracy: this.isHighAccuracySelect,
          geocode: this.geocodeSelect,
          success: function (res : any) {
            uni.hideLoading()
            console.log(res);
            this.exeRet = JSON.stringify(res)
          },
          fail: function (res : any) {
            uni.hideLoading()
            console.log(res);
            this.exeRet = JSON.stringify(res)
          },
          complete: function (res : any) {
            uni.hideLoading()
            console.log(res);
            this.exeRet = JSON.stringify(res)
          }
        }));


      }

杜庆泉's avatar
杜庆泉 已提交
111
    }
杜庆泉's avatar
杜庆泉 已提交
112 113
  }
</script>
杜庆泉's avatar
杜庆泉 已提交
114 115 116
<style>
@import '@/common/uni-uvue.css';
</style>