-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
114 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { GeoCoord } from './GeoCoord' | ||
|
||
describe('GeoCoord', () => { | ||
test('ok', () => { | ||
expect( | ||
GeoCoord.WGS84ToGCJ02({ | ||
longitude: 238, | ||
latitude: 34, | ||
}), | ||
).toMatchSnapshot() | ||
}) | ||
}) |
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,87 @@ | ||
import gcoord from 'gcoord' | ||
import type { CRSTypes } from 'gcoord/dist/types/crs/index' | ||
|
||
export interface GeoCoordTransformInput { | ||
/** | ||
* 经度。 | ||
*/ | ||
longitude: number | ||
|
||
/** | ||
* 纬度。 | ||
*/ | ||
latitude: number | ||
} | ||
|
||
export interface GeoCoordTransformOutput { | ||
/** | ||
* 经度。 | ||
*/ | ||
longitude: number | ||
|
||
/** | ||
* 纬度。 | ||
*/ | ||
latitude: number | ||
} | ||
|
||
const makeTransformer = | ||
(from: CRSTypes, to: CRSTypes) => | ||
(input: GeoCoordTransformInput): GeoCoordTransformOutput => { | ||
const res = gcoord.transform([input.longitude, input.latitude], from, to) | ||
return { | ||
longitude: res[0], | ||
latitude: res[1], | ||
} | ||
} | ||
|
||
/** | ||
* 地理坐标系转换工具。 | ||
* | ||
* - `大地坐标系(WGS84 坐标系)`: GPS 全球卫星定位系统使用的坐标系; | ||
* - `火星坐标系(GCJ02 坐标系)`: 腾讯地图、高德地图等使用的坐标系,是由中国国家测绘局制定的由 WGS84 加密后得到的坐标系; | ||
* - `百度坐标系(BD09 坐标系)`: 百度地图使用的坐标系,是在 GCJ02 基础上再次加密得到的坐标系。 | ||
*/ | ||
export class GeoCoord { | ||
/** | ||
* `WGS84 坐标系` 转 `GCJ02 坐标系`。 | ||
* | ||
* 应用场景:GPS 坐标转腾讯地图、高德地图坐标。 | ||
*/ | ||
static WGS84ToGCJ02 = makeTransformer(gcoord.WGS84, gcoord.GCJ02) | ||
|
||
/** | ||
* `WGS84 坐标系` 转 `BD09 坐标系`。 | ||
* | ||
* 应用场景:GPS 坐标转百度地图坐标。 | ||
*/ | ||
static WGS84ToBD09 = makeTransformer(gcoord.WGS84, gcoord.BD09) | ||
|
||
/** | ||
* `GCJ02 坐标系` 转 `WGS84 坐标系`。 | ||
* | ||
* 应用场景:腾讯地图、高德地图坐标转 GPS 坐标。 | ||
*/ | ||
static GCJ02ToWGS84 = makeTransformer(gcoord.GCJ02, gcoord.WGS84) | ||
|
||
/** | ||
* `GCJ02 坐标系` 转 `BD09 坐标系`。 | ||
* | ||
* 应用场景:腾讯地图、高德地图坐标转百度地图坐标。 | ||
*/ | ||
static GCJ02ToBD09 = makeTransformer(gcoord.GCJ02, gcoord.BD09) | ||
|
||
/** | ||
* `BD09 坐标系` 转 `WGS84 坐标系`。 | ||
* | ||
* 应用场景:百度地图坐标转 GPS 坐标。 | ||
*/ | ||
static BD09ToWGS84 = makeTransformer(gcoord.BD09, gcoord.WGS84) | ||
|
||
/** | ||
* `BD09 坐标系` 转 `GCJ02 坐标系`。 | ||
* | ||
* 应用场景:百度地图坐标转腾讯地图、高德地图坐标。 | ||
*/ | ||
static BD09ToGCJ02 = makeTransformer(gcoord.BD09, gcoord.GCJ02) | ||
} |
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,8 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`GeoCoord ok 1`] = ` | ||
Object { | ||
"latitude": 34, | ||
"longitude": 238, | ||
} | ||
`; |
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