提交 e7992111 编写于 作者: M mehaotian

feat(App): location

上级 382e275d
const PI: number = 3.1415926535897932384626
const a: number = 6378245.0
const ee: number = 0.00669342162296594323
export function gcj02towgs84(lng: number, lat: number) {
lat = +lat
lng = +lng
if (outOfChina(lng, lat)) {
return [lng, lat]
}
let dlat = _transformlat(lng - 105.0, lat - 35.0)
let dlng = _transformlng(lng - 105.0, lat - 35.0)
const radlat = (lat / 180.0) * PI
let magic = Math.sin(radlat)
magic = 1 - ee * magic * magic
const sqrtmagic = Math.sqrt(magic)
dlat = (dlat * 180.0) / (((a * (1 - ee)) / (magic * sqrtmagic)) * PI)
dlng = (dlng * 180.0) / ((a / sqrtmagic) * Math.cos(radlat) * PI)
const mglat = lat + dlat
const mglng = lng + dlng
return [lng * 2 - mglng, lat * 2 - mglat]
}
export function wgs84togcj02(lng: number, lat: number) {
lat = +lat
lng = +lng
if (outOfChina(lng, lat)) {
return [lng, lat]
}
let dlat = _transformlat(lng - 105.0, lat - 35.0)
let dlng = _transformlng(lng - 105.0, lat - 35.0)
const radlat = (lat / 180.0) * PI
let magic = Math.sin(radlat)
magic = 1 - ee * magic * magic
const sqrtmagic = Math.sqrt(magic)
dlat = (dlat * 180.0) / (((a * (1 - ee)) / (magic * sqrtmagic)) * PI)
dlng = (dlng * 180.0) / ((a / sqrtmagic) * Math.cos(radlat) * PI)
const mglat = lat + dlat
const mglng = lng + dlng
return [mglng, mglat]
}
const outOfChina = function (lng: number, lat: number) {
return (
lng < 72.004 || lng > 137.8347 || lat < 0.8293 || lat > 55.8271 || false
)
}
const _transformlat = function (lng: number, lat: number) {
let ret =
-100.0 +
2.0 * lng +
3.0 * lat +
0.2 * lat * lat +
0.1 * lng * lat +
0.2 * Math.sqrt(Math.abs(lng))
ret +=
((20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) *
2.0) /
3.0
ret +=
((20.0 * Math.sin(lat * PI) + 40.0 * Math.sin((lat / 3.0) * PI)) * 2.0) /
3.0
ret +=
((160.0 * Math.sin((lat / 12.0) * PI) + 320 * Math.sin((lat * PI) / 30.0)) *
2.0) /
3.0
return ret
}
const _transformlng = function (lng: number, lat: number) {
let ret =
300.0 +
lng +
2.0 * lat +
0.1 * lng * lng +
0.1 * lng * lat +
0.1 * Math.sqrt(Math.abs(lng))
ret +=
((20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) *
2.0) /
3.0
ret +=
((20.0 * Math.sin(lng * PI) + 40.0 * Math.sin((lng / 3.0) * PI)) * 2.0) /
3.0
ret +=
((150.0 * Math.sin((lng / 12.0) * PI) +
300.0 * Math.sin((lng / 30.0) * PI)) *
2.0) /
3.0
return ret
}
......@@ -13,3 +13,5 @@ export * from './keyboard/keyboard'
export * from './network/downloadFile'
export * from './context/createInnerAudioContext'
export * from './location/getLocation'
import {
defineAsyncApi,
API_GET_LOCATION,
API_TYPE_GET_LOCATION,
GetLocationProtocol,
GetLocationOptions,
} from '@dcloudio/uni-api'
import { gcj02towgs84, wgs84togcj02 } from '../../../helpers/location'
function getLocationSuccess(
type: string,
position: any,
resolve: (res: any) => void
) {
const coords = position.coords
if (type !== position.coordsType) {
if (process.env.NODE_ENV !== 'production') {
console.log(
`UNIAPP[location]:before[${position.coordsType}][lng:${coords.longitude},lat:${coords.latitude}]`
)
}
let coordArray
if (type === 'wgs84') {
coordArray = gcj02towgs84(coords.longitude, coords.latitude)
} else if (type === 'gcj02') {
coordArray = wgs84togcj02(coords.longitude, coords.latitude)
}
if (coordArray) {
coords.longitude = coordArray[0]
coords.latitude = coordArray[1]
if (process.env.NODE_ENV !== 'production') {
console.log(
`UNIAPP[location]:after[${type}][lng:${coords.longitude},lat:${coords.latitude}]`
)
}
}
}
resolve({
type,
altitude: coords.altitude || 0,
latitude: coords.latitude,
longitude: coords.longitude,
speed: coords.speed,
accuracy: coords.accuracy,
address: position.address,
errMsg: 'getLocation:ok',
})
}
export const getLocation = <API_TYPE_GET_LOCATION>defineAsyncApi(
API_GET_LOCATION,
(
{ type = 'wgs84', geocode = false, altitude = false },
{ resolve, reject }
) => {
plus.geolocation.getCurrentPosition(
(position) => {
getLocationSuccess(type, position, resolve)
},
(e) => {
// 坐标地址解析失败
if (e.code === 1501) {
getLocationSuccess(type, e, resolve)
return
}
reject('getLocation:fail ' + e.message)
},
{
geocode: geocode,
enableHighAccuracy: altitude,
}
)
},
GetLocationProtocol,
GetLocationOptions
)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册