Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(h5):添加非微信JS-SDK下的getLocation API实现 #11616

Merged
merged 13 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
CaiDingxian marked this conversation as resolved.
Show resolved Hide resolved
})
}

// 开始获取位置
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'
CaiDingxian marked this conversation as resolved.
Show resolved Hide resolved
}
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')
CaiDingxian marked this conversation as resolved.
Show resolved Hide resolved
} 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'