提交 f5828da0 编写于 作者: 雪洛's avatar 雪洛

fix: getLocation在非app端去除provider逻辑

上级 b7533be5
<template> <template>
<!-- #ifdef APP --> <!-- #ifdef APP -->
<scroll-view style="flex: 1;"> <scroll-view style="flex: 1;">
<!-- #endif --> <!-- #endif -->
<page-head :title="title"></page-head> <page-head :title="title"></page-head>
<view style="padding: 4px"> <view style="padding: 4px">
<text class="hello-text"> <text class="hello-text">
定位功能默认调用操作系统定位API实现, 也支持腾讯定位。\n 定位功能默认调用操作系统定位API实现, 也支持腾讯定位。\n
部分手机因gms兼容不好可能导致无法使用系统定位, gcj国标、逆地理信息等功能需调用内置腾讯定位。</text> 部分手机因gms兼容不好可能导致无法使用系统定位, gcj国标、逆地理信息等功能需调用内置腾讯定位。</text>
</view> </view>
<view class="uni-padding-wrap uni-common-mt"> <view class="uni-padding-wrap uni-common-mt">
<view class="uni-list-cell-db">定位服务商provider(如系统定位,腾讯定位等)</view> <!-- #ifdef APP -->
<view class="uni-list" style="margin-bottom: 20px"> <view class="uni-list-cell-db">定位服务商provider(如系统定位,腾讯定位等)</view>
<radio-group @change="radioChangePV"> <view class="uni-list" style="margin-bottom: 20px">
<radio class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in providerList" :key="item.id" <radio-group @change="radioChangePV">
:class="index < providerList.length - 1 ? 'uni-list-cell-line' : ''" :value="item.id" <radio class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in providerList" :key="item.id"
:checked="index === currentProvider"> :class="index < providerList.length - 1 ? 'uni-list-cell-line' : ''" :value="item.id"
{{ item.name }} :checked="index === currentProvider">
</radio> {{ item.name }}
</radio-group> </radio>
</view> </radio-group>
<view class="uni-list-cell-db">定位类型</view> </view>
<view class="uni-list"> <!-- #endif -->
<radio-group @change="radioChange"> <view class="uni-list-cell-db">定位类型</view>
<radio class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in items" :key="item.value" <view class="uni-list">
:class="index < items.length - 1 ? 'uni-list-cell-line' : ''" :value="item.value" <radio-group @change="radioChange">
:checked="index === current"> <radio class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in items" :key="item.value"
{{ item.name }} :class="index < items.length - 1 ? 'uni-list-cell-line' : ''" :value="item.value"
</radio> :checked="index === current">
</radio-group> {{ item.name }}
</view> </radio>
<view class="uni-list-cell uni-list-cell-pd" style="margin-top: 20px"> </radio-group>
<view class="uni-list-cell-db">高度信息</view> </view>
<switch :checked="altitudeSelect" @change="altitudeChange" /> <view class="uni-list-cell uni-list-cell-pd" style="margin-top: 20px">
</view> <view class="uni-list-cell-db">高度信息</view>
<view class="uni-list-cell uni-list-cell-pd"> <switch :checked="altitudeSelect" @change="altitudeChange" />
<view class="uni-list-cell-db">开启高精度定位</view> </view>
<switch :checked="isHighAccuracySelect" @change="highAccuracySelectChange" /> <view class="uni-list-cell uni-list-cell-pd">
</view> <view class="uni-list-cell-db">开启高精度定位</view>
<view class="uni-list-cell uni-list-cell-pd"> <switch :checked="isHighAccuracySelect" @change="highAccuracySelectChange" />
<view class="uni-list-cell-db">是否解析地址信息</view> </view>
<switch :checked="geocodeSelect" @change="geocodeChange" /> <view class="uni-list-cell uni-list-cell-pd">
</view> <view class="uni-list-cell-db">是否解析地址信息</view>
<text>{{ exeRet }}</text> <switch :checked="geocodeSelect" @change="geocodeChange" />
<view class="uni-btn-v"> </view>
<button class="uni-btn" type="default" @tap="getLocationTap"> <text>{{ exeRet }}</text>
获取定位 <view class="uni-btn-v">
</button> <button class="uni-btn" type="default" @tap="getLocationTap">
</view> 获取定位
</view> </button>
<!-- #ifdef APP --> </view>
</scroll-view> </view>
<!-- #endif --> <!-- #ifdef APP -->
</template> </scroll-view>
<script lang="uts"> <!-- #endif -->
export type LocationItem = { id : string, name : string, provider ?: UniProvider } </template>
export type ItemType = { value : string, name : string } <script lang="uts">
export type LocationItem = { id : string, name : string, provider ?: UniProvider }
export default { export type ItemType = { value : 'wgs84' | 'gcj02', name : 'wgs84' | 'gcj02' }
data() {
return { export default {
title: 'get-location', data() {
altitudeSelect: false, return {
isHighAccuracySelect: false, title: 'get-location',
geocodeSelect: false, altitudeSelect: false,
exeRet: '', isHighAccuracySelect: false,
items: [ geocodeSelect: false,
{ exeRet: '',
value: 'wgs84', items: [
name: 'wgs84' {
}, value: 'wgs84',
{ name: 'wgs84'
value: 'gcj02', },
name: 'gcj02' {
} value: 'gcj02',
] as ItemType[], name: 'gcj02'
providerList: [] as LocationItem[], }
current: 0, ] as ItemType[],
currentProvider: 0 providerList: [] as LocationItem[],
} current: 0,
}, currentProvider: 0
onLoad: function () { }
this.getProvider() },
}, onLoad: function () {
methods: { // #ifdef APP
getProvider() { this.getProvider()
uni.getProvider({ // #endif
service: "location", },
success: (e) => { methods: {
console.log("location success:" + JSON.stringify(e), e.providerObjects.length); getProvider() {
let array = e.providerIds as string[] // #ifdef APP
array.forEach((value : string) => { uni.getProvider({
let locationProvider = e.providerObjects.find((item) : boolean => { service: "location",
return item.id == value success: (e) => {
}) console.log("location success:" + JSON.stringify(e), e.providerObjects.length);
if (locationProvider != null) { let array = e.providerIds as string[]
this.providerList.push({ array.forEach((value : string) => {
name: locationProvider.description, let locationProvider = e.providerObjects.find((item) : boolean => {
id: locationProvider.id, return item.id == value
provider: e.providerObjects.find((item) : boolean => { })
return item.id == locationProvider.id if (locationProvider != null) {
}) this.providerList.push({
} as LocationItem); name: locationProvider.description,
} id: locationProvider.id,
}) provider: e.providerObjects.find((item) : boolean => {
}, return item.id == locationProvider.id
fail: (e) => { })
console.log("获取支付通道失败:", e); } as LocationItem);
} }
}); })
},
fail: (e) => {
console.log("获取支付通道失败:", e);
}
});
this.providerList.forEach((value, index) => { this.providerList.forEach((value, index) => {
if (value.id == "system") { if (value.id == "system") {
this.currentProvider = index this.currentProvider = index
} }
}) })
}, // #endif
altitudeChange: function (e : UniSwitchChangeEvent) { },
this.altitudeSelect = e.detail.value altitudeChange: function (e : UniSwitchChangeEvent) {
}, this.altitudeSelect = e.detail.value
geocodeChange: function (e : UniSwitchChangeEvent) { },
this.geocodeSelect = e.detail.value geocodeChange: function (e : UniSwitchChangeEvent) {
}, this.geocodeSelect = e.detail.value
highAccuracySelectChange: function (e : UniSwitchChangeEvent) { },
this.isHighAccuracySelect = e.detail.value highAccuracySelectChange: function (e : UniSwitchChangeEvent) {
}, this.isHighAccuracySelect = e.detail.value
radioChange(e : UniRadioGroupChangeEvent) { },
for (let i = 0; i < this.items.length; i++) { radioChange(e : UniRadioGroupChangeEvent) {
if (this.items[i].value === e.detail.value) { for (let i = 0; i < this.items.length; i++) {
this.current = i; if (this.items[i].value === e.detail.value) {
break; this.current = i;
} break;
} }
}, }
radioChangePV(e : UniRadioGroupChangeEvent) { },
for (let i = 0; i < this.providerList.length; i++) { radioChangePV(e : UniRadioGroupChangeEvent) {
if (this.providerList[i].id === e.detail.value) { for (let i = 0; i < this.providerList.length; i++) {
this.currentProvider = i; if (this.providerList[i].id === e.detail.value) {
break; this.currentProvider = i;
} break;
}
} }
if (e.detail.value == "system") { if (e.detail.value == "system") {
this.current = 0 this.current = 0
} else if (e.detail.value == "tencent") { } else if (e.detail.value == "tencent") {
this.current = 1 this.current = 1
} }
}, },
getLocationTap: function () { getLocationTap: function () {
// #ifdef APP
if (this.providerList.length == 0) { if (this.providerList.length == 0) {
uni.showToast({ uni.showToast({
title: '未获取到provider,请确定基座中包含location功能', title: '未获取到provider,请确定基座中包含location功能',
icon: "error" icon: "error"
}) })
console.log("未获取到provider,请确定基座中包含location功能") console.log("未获取到provider,请确定基座中包含location功能")
return return
} }
uni.showLoading({ // #endif
title: '定位中' uni.showLoading({
}) title: '定位中'
uni.getLocation(({ })
provider: this.providerList[this.currentProvider].id, uni.getLocation(({
type: this.items[this.current].value, // #ifdef APP
altitude: this.altitudeSelect, provider: this.providerList[this.currentProvider].id,
isHighAccuracy: this.isHighAccuracySelect, // #endif
geocode: this.geocodeSelect, type: this.items[this.current].value,
success: (res : any) => { altitude: this.altitudeSelect,
uni.hideLoading() isHighAccuracy: this.isHighAccuracySelect,
console.log(res); geocode: this.geocodeSelect,
this.exeRet = JSON.stringify(res) success: (res : any) => {
}, uni.hideLoading()
fail: (res : any) => { console.log(res);
uni.hideLoading() this.exeRet = JSON.stringify(res)
console.log(res); },
this.exeRet = JSON.stringify(res) fail: (res : any) => {
}, uni.hideLoading()
complete: (res : any) => { console.log(res);
uni.hideLoading() this.exeRet = JSON.stringify(res)
console.log(res); },
this.exeRet = JSON.stringify(res) complete: (res : any) => {
} uni.hideLoading()
})); console.log(res);
this.exeRet = JSON.stringify(res)
}
} }));
}
} }
}
}
</script> </script>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册