-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(h5):添加非微信JS-SDK下的getLocation API实现
- Loading branch information
1 parent
61c4112
commit 0cc82c1
Showing
2 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters