forked from NervJS/taro
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #190 from xiaoyan428820/release-3.6.8-app
fix: 位置模块接口实现问题及样例修改
- Loading branch information
Showing
8 changed files
with
379 additions
and
64 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
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
76 changes: 76 additions & 0 deletions
76
packages/taro-mpharmony/src/api/location/getFuzzyLocation.ts
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,76 @@ | ||
import Taro from '@tarojs/taro' | ||
import { shouldBeObject } from 'src/utils' | ||
import { wgs84Togcj02 } from 'src/utils/coordinateConvert' | ||
|
||
import { MethodHandler } from '../../utils/handler' | ||
|
||
export const getFuzzyLocation: typeof Taro.getFuzzyLocation = (options) => { | ||
const name = 'getFuzzyLocation' | ||
// options must be an Object | ||
const isObject = shouldBeObject(options) | ||
if (!isObject.flag) { | ||
const res = { errMsg: `${name}:fail ${isObject.msg}` } | ||
console.error(res.errMsg) | ||
return Promise.reject(res) | ||
} | ||
const { | ||
type = 'wgs84', | ||
success, | ||
fail, | ||
complete | ||
} = options as Exclude<typeof options, undefined> | ||
const handle = new MethodHandler({ name, success, fail, complete }) | ||
|
||
return new Promise<Taro.getFuzzyLocation.SuccessCallbackResult>((resolve, reject) => { | ||
const fuzzyLocation: Partial<Taro.getFuzzyLocation.SuccessCallbackResult> = {} | ||
let flag = true | ||
|
||
// 默认10秒未获取则返回超时 | ||
const timeoutId = setTimeout(function () { | ||
if (!fuzzyLocation.latitude && !fuzzyLocation.longitude) { | ||
const result: TaroGeneral.CallbackResult = { | ||
errMsg: '定位超时!' | ||
} | ||
flag = false | ||
handle.fail(result, { resolve, reject }) | ||
} | ||
}, 10000) | ||
|
||
// @ts-ignore | ||
native.getLocation({ | ||
success: (res: any) => { | ||
// 超时后即使后面回调触发了也不后面的逻辑 | ||
if (!flag) { | ||
return | ||
} | ||
const lat = res.latitude | ||
const lng = res.longitude | ||
// 鸿蒙默认返回的是wgs84坐标 | ||
if (type === 'gcj02') { | ||
const ret = wgs84Togcj02(lng, lat) | ||
fuzzyLocation.latitude = parseFloat(ret[1].toFixed(6)) | ||
fuzzyLocation.longitude = parseFloat(ret[0].toFixed(6)) | ||
} else if (type === 'wgs84') { | ||
fuzzyLocation.latitude = parseFloat(lat.toFixed(6)) | ||
fuzzyLocation.longitude = parseFloat(lng.toFixed(6)) | ||
} else { | ||
const result: TaroGeneral.CallbackResult = { | ||
errMsg: 'type参数有误,仅支持"wgs84"和"gcj02"坐标系!' | ||
} | ||
clearTimeout(timeoutId) | ||
return handle.fail(result, { resolve, reject }) | ||
} | ||
handle.success(fuzzyLocation, { resolve, reject }) | ||
}, | ||
fail: (res: any) => { | ||
if (!flag) { | ||
return | ||
} | ||
const result: TaroGeneral.CallbackResult = { | ||
errMsg: `${name}:fail errCode: ${res.errCode}` | ||
} | ||
handle.fail(result, { resolve, reject }) | ||
} | ||
}) | ||
}) | ||
} |
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
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 |
---|---|---|
@@ -1,22 +1,16 @@ | ||
import { processOpenApi, temporarilyNotSupport } from '../../utils/index' | ||
import { temporarilyNotSupport } from '../../utils/index' | ||
|
||
// 位置 | ||
export * from './startLocationUpdate' | ||
export * from './startLocationUpdateBackground' | ||
export * from './stopLocationUpdate' | ||
|
||
export const openLocation = /* @__PURE__ */ processOpenApi({ | ||
name: 'openLocation', | ||
defaultOptions: { scale: 18 }, | ||
}) | ||
|
||
export { getLocation } from './getLocation' | ||
export * from './chooseLocation' | ||
export * from './getFuzzyLocation' | ||
export * from './getLocation' | ||
export * from './offLocationChange' | ||
export * from './offLocationChangeError' | ||
export * from './onLocationChange' | ||
export * from './onLocationChangeError' | ||
export * from './openLocation' | ||
export * from './startLocationUpdate' | ||
export * from './startLocationUpdateBackground' | ||
export * from './stopLocationUpdate' | ||
|
||
export const choosePoi = /* @__PURE__ */ temporarilyNotSupport('choosePoi') | ||
export const getFuzzyLocation = /* @__PURE__ */ temporarilyNotSupport('getFuzzyLocation') | ||
|
||
export { chooseLocation } from './chooseLocation' | ||
export const choosePoi = /* @__PURE__ */ temporarilyNotSupport('choosePoi') |
Oops, something went wrong.