get-location.uvue 6.7 KB
Newer Older
1
<template>
2 3 4 5 6 7
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1;">
  <!-- #endif -->
    <page-head :title="title"></page-head>
    <view style="padding: 4px">
      <text class="hello-text">
8 9
        定位功能默认调用操作系统定位API实现, 也支持腾讯定位。\n
        部分手机因gms兼容不好可能导致无法使用系统定位, gcj国标、逆地理信息等功能需调用内置腾讯定位。</text>
10
    </view>
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

    <view class="uni-padding-wrap uni-common-mt">
      <view class="uni-list-cell-db">定位服务商provider(如系统定位,腾讯定位等)</view>
      <view class="uni-list" style="margin-bottom: 20px">
        <radio-group @change="radioChangePV">
          <radio class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in providerList" :key="item.id"
            :class="index < providerList.length - 1 ? 'uni-list-cell-line' : ''" :value="item.id"
            :checked="index === currentProvider">
            {{ item.name }}
          </radio>
        </radio-group>
      </view>
      <view class="uni-list-cell-db">定位类型</view>
      <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" style="margin-top: 20px">
        <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>
51
    </view>
52 53 54
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
55 56 57 58 59 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 111 112 113
</template>
<script lang="uts">
  export type LocationItem = { id : string, name : string, provider ?: UniProvider }
  export type ItemType = { value : string, name : string }

  export default {
    data() {
      return {
        title: 'get-location',
        altitudeSelect: false,
        isHighAccuracySelect: false,
        geocodeSelect: false,
        exeRet: '',
        items: [
          {
            value: 'wgs84',
            name: 'wgs84'
          },
          {
            value: 'gcj02',
            name: 'gcj02'
          }
        ] as ItemType[],
        providerList: [] as LocationItem[],
        current: 0,
        currentProvider: 0
      }
    },
    onLoad: function () {
      this.getProvider()
    },
    methods: {
      getProvider() {
        uni.getProvider({
          service: "location",
          success: (e) => {
            console.log("location success:" + JSON.stringify(e), e.providers.length);
            let array = e.provider as string[]
            array.forEach((value : string) => {
              let locationProvider = e.providers.find((item) : boolean => {
                return item.id == value
              })
              if (locationProvider != null) {
                this.providerList.push({
                  name: locationProvider.description,
                  id: locationProvider.id,
                  provider: e.providers.find((item) : boolean => {
                    return item.id == locationProvider.id
                  })
                } as LocationItem);
              }
            })
          },
          fail: (e) => {
            console.log("获取支付通道失败:", e);
          }
        });


114 115 116 117 118
        this.providerList.forEach((value, index) => {
          if (value.id == "system") {
            this.currentProvider = index
          }
        })
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
      },
      altitudeChange: function (e : UniSwitchChangeEvent) {
        this.altitudeSelect = e.detail.value
      },
      geocodeChange: function (e : UniSwitchChangeEvent) {
        this.geocodeSelect = e.detail.value
      },
      highAccuracySelectChange: function (e : UniSwitchChangeEvent) {
        this.isHighAccuracySelect = e.detail.value
      },
      radioChange(e : UniRadioGroupChangeEvent) {
        for (let i = 0; i < this.items.length; i++) {
          if (this.items[i].value === e.detail.value) {
            this.current = i;
            break;
          }
        }
      },
      radioChangePV(e : UniRadioGroupChangeEvent) {
        for (let i = 0; i < this.providerList.length; i++) {
          if (this.providerList[i].id === e.detail.value) {
            this.currentProvider = i;
            break;
          }
        }
      },
      getLocationTap: function () {
146 147 148 149 150 151
        if (this.providerList.length == 0) {
          uni.showToast({
            title: '未获取到provider,请确定基座中包含location功能',
            icon: "error"
          })
          console.log("未获取到provider,请确定基座中包含location功能")
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
          return
        }
        uni.showLoading({
          title: '定位中'
        })
        console.log("provider", this.providerList[this.currentProvider].id)
        let locationProvider = this.providerList[this.currentProvider]

        if (locationProvider.provider?.isAppExist == false && 'tencent' == locationProvider.provider?.id) {
          uni.showToast({
            title: '需要打自定义基座',
            icon: "error"
          })
          return
        }
        uni.getLocation(({
          provider: this.providerList[this.currentProvider].id,
          type: this.items[this.current].value,
          altitude: this.altitudeSelect,
          isHighAccuracy: this.isHighAccuracySelect,
          geocode: this.geocodeSelect,
          success: (res : any) => {
            uni.hideLoading()
            console.log(res);
            this.exeRet = JSON.stringify(res)
          },
          fail: (res : any) => {
            uni.hideLoading()
            console.log(res);
            this.exeRet = JSON.stringify(res)
          },
          complete: (res : any) => {
            uni.hideLoading()
            console.log(res);
            this.exeRet = JSON.stringify(res)
          }
        }));


      }

    }
  }
杜庆泉's avatar
杜庆泉 已提交
195
</script>