Skip to content

Commit

Permalink
feat(h5):添加非微信JS-SDK下的getLocation API实现
Browse files Browse the repository at this point in the history
  • Loading branch information
CaiDingxian committed Apr 11, 2022
1 parent 61c4112 commit 0cc82c1
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
88 changes: 88 additions & 0 deletions packages/taro-h5/src/api/location/getLocation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import Taro from '@tarojs/api'
import { processOpenApi, shouldBeObject } from '../utils'
import { MethodHandler } from '../utils/handler'

const getLocationInWeb: (options: Taro.getLocation.Option) => Promise<Taro.getLocation.SuccessCallbackResult | undefined> = (options: Taro.getLocation.Option): Promise<Taro.getLocation.SuccessCallbackResult | undefined> => {
// 断言 options 必须是 Object
const isObject = shouldBeObject(options)
if (!isObject.flag) {
const res = { errMsg: `getLocation:fail ${isObject.msg}` }
console.error(res.errMsg)
return Promise.reject(res)
}

// 解构回调函数
const { success, fail, complete } = options

const handle = new MethodHandler({ name: 'getLocation', success, fail, complete })

// let defaultMaximumAge = 5 * 1000 //允许取多久以内的缓存位置,Taro接口参数中没有定义

const positionOptions: PositionOptions = {

enableHighAccuracy: options.isHighAccuracy || (options.altitude != null), // 海拔定位需要高精度
timeout: options.highAccuracyExpireTime // 高精度定位超时时间
// maximumAge: defaultMaximumAge //允许取多久以内的缓存位置
}

// Web端API实现暂时仅支持GPS坐标系
if (options.type!.toUpperCase() !== 'WGS84') {
return handle.fail({
errMsg: 'This coordinate system type is not temporarily supported"'
})
}

// 判断当前浏览器是否支持位置API
const geolocationSupported = navigator.geolocation

if (!geolocationSupported) {
return handle.fail({
errMsg: 'getLocation:fail The current browser does not support this feature'
})
}

// 开始获取位置
return new Promise<Taro.getLocation.SuccessCallbackResult | undefined>(
(resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => {
const result: Taro.getLocation.SuccessCallbackResult = {
/** 位置的精确度 */
accuracy: position.coords.accuracy,
/** 高度,单位 m */
altitude: position.coords.altitude!,
/** 水平精度,单位 m */
horizontalAccuracy: position.coords.accuracy,
/** 纬度,范围为 -90~90,负数表示南纬 */
latitude: position.coords.latitude,
/** 经度,范围为 -180~180,负数表示西经 */
longitude: position.coords.longitude,
/** 速度,单位 m/s */
speed: position.coords.speed!,
/** 垂直精度,单位 m(Android 无法获取,返回 0) */
verticalAccuracy: position.coords.altitudeAccuracy || 0,
/** 调用结果 */
errMsg: 'getLocation:ok'
}
handle.success(result, resolve)
},
(error) => {
handle.fail({ errMsg: error.message }, reject)
},
positionOptions
)
}
)
}

const getGetLocationImpl = () => {
// @ts-ignore
// 在微信JS-SDK环境下调用openApi,否则调用Web API
if (window.wx) {
return processOpenApi('getLocation')
} else {
return getLocationInWeb
}
}

export const getLocation = getGetLocationImpl()
4 changes: 2 additions & 2 deletions packages/taro-h5/src/api/location/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export const onLocationChange = temporarilyNotSupport('onLocationChange')
export const offLocationChangeError = temporarilyNotSupport('offLocationChangeError')
export const offLocationChange = temporarilyNotSupport('offLocationChange')

export const getLocation = processOpenApi('getLocation')

export const choosePoi = temporarilyNotSupport('choosePoi')

export * from './chooseLocation'

export * from './getLocation'

0 comments on commit 0cc82c1

Please sign in to comment.