瀏覽代碼

完善百度地图的接入

tags/1.15.0
Caven Chen 4 年之前
父節點
當前提交
61ad9bcdc3

+ 31
- 23
src/core/imagery/projection/BaiduMercatorProjection.js 查看文件

@@ -160,6 +160,12 @@ class BaiduMercatorProjection {
this.isWgs84 = false
}

/**
*
* @param point1
* @param point2
* @returns {number}
*/
getDistanceByMC(point1, point2) {
if (!point1 || !point2) {
return 0
@@ -180,10 +186,10 @@ class BaiduMercatorProjection {
}

/**
* 根据经纬度坐标计算两点间距离;
* Calculate the distance between two points according to the latitude and longitude coordinates
* @param point1
* @param point2
* @returns {number|*} 返回两点间的距离
* @returns {number|*}
*/
getDistanceByLL(point1, point2) {
if (!point1 || !point2) {
@@ -201,7 +207,7 @@ class BaiduMercatorProjection {
}

/**
* 平面直角坐标转换成经纬度坐标;
* The plane cartesian coordinates are converted to latitude and longitude coordinates
* @param point
* @returns {Point|{lng: number, lat: number}}
*/
@@ -242,8 +248,8 @@ class BaiduMercatorProjection {
}

/**
* 经纬度坐标转换成平面直角坐标;
* @param point 经纬度坐标
* The latitude and longitude coordinates are converted to plane cartesian coordinates
* @param point
* @returns {{lng: number, lat: number}|*}
*/
convertLL2MC(point) {
@@ -419,18 +425,18 @@ class BaiduMercatorProjection {
}

/**
* 墨卡托变换至经纬度
* @param point 墨卡托
* @returns Point 经纬度
* WebMercator transforms to latitude and longitude
* @param point
* @returns {Point|{lng: number, lat: number}}
*/
mercatorToLngLat(point) {
return this.convertMC2LL(point)
}

/**
* 平面到球面坐标
* @param point 平面坐标
* @returns Point 球面坐标
*
* @param point
* @returns {Point|{lng: number, lat: number}}
*/
pointToLngLat(point) {
let mercator = { lng: point.x, lat: point.y }
@@ -438,15 +444,16 @@ class BaiduMercatorProjection {
}

/**
* 地理坐标转换至像素坐标
* @param point 地理坐标
* @param zoom 级别
* @param mapCenter 地图中心点,注意为了保证没有误差,这里需要传递墨卡托坐标
* @param mapSize 地图容器大小
* Latitude and longitude coordinates transforms to pixel coordinates
* @param point
* @param zoom
* @param mapCenter
* @param mapSize
* @returns {{x: number, y: number}}
*/
pointToPixel(point, zoom, mapCenter, mapSize) {
if (!point) {
return
return { x: 0, y: 0 }
}
point = this.lngLatToMercator(point)
let zoomUnits = this.getZoomUnits(zoom)
@@ -460,15 +467,16 @@ class BaiduMercatorProjection {
}

/**
* 像素坐标转换至地理坐标
* @param pixel 像素坐标
* @param zoom 级别
* @param mapCenter 地图中心点,注意为了保证没有误差,这里需要传递墨卡托坐标
* @param mapSize 地图容器大小
* Pixel coordinates transforms to latitude and longitude coordinates
* @param pixel
* @param zoom
* @param mapCenter
* @param mapSize
* @returns {Point|{lng: number, lat: number}}
*/
pixelToPoint(pixel, zoom, mapCenter, mapSize) {
if (!pixel) {
return
return { lng: 0, lat: 0 }
}
let zoomUnits = this.getZoomUnits(zoom)
let lng = mapCenter['lng'] + zoomUnits * (pixel.x - mapSize.width / 2)

+ 32
- 17
src/core/imagery/provider/BaiduImageryProvider.js 查看文件

@@ -34,22 +34,29 @@ class BaiduImageryProvider {
this._tileWidth = 256
this._tileHeight = 256
this._maximumLevel = 18
let resolutions = []
for (let i = 0; i < 19; i++) {
resolutions[i] = 256 * Math.pow(2, 18 - i)
this._crs = options.crs || 'BD09'
if (options.crs === 'WGS84') {
let resolutions = []
for (let i = 0; i < 19; i++) {
resolutions[i] = 256 * Math.pow(2, 18 - i)
}
this._tilingScheme = new BaiduMercatorTilingScheme({
resolutions,
rectangleSouthwestInMeters: new Cesium.Cartesian2(
-20037726.37,
-12474104.17
),
rectangleNortheastInMeters: new Cesium.Cartesian2(
20037726.37,
12474104.17
)
})
} else {
this._tilingScheme = new Cesium.WebMercatorTilingScheme({
rectangleSouthwestInMeters: new Cesium.Cartesian2(-33554054, -33746824),
rectangleNortheastInMeters: new Cesium.Cartesian2(33554054, 33746824)
})
}
this._tilingScheme = new BaiduMercatorTilingScheme({
rectangleSouthwestInMeters: new Cesium.Cartesian2(
-20037726.37,
-12474104.17
),
rectangleNortheastInMeters: new Cesium.Cartesian2(
20037726.37,
12474104.17
),
resolutions,
crs: options.crs || ''
})
this._rectangle = this._tilingScheme.rectangle
this._credit = undefined
this._token = undefined
@@ -145,14 +152,22 @@ class BaiduImageryProvider {
'requestImage must not be called before the imagery provider is ready.'
)
}
let xTiles = this._tilingScheme.getNumberOfXTilesAtLevel(level)
let yTiles = this._tilingScheme.getNumberOfYTilesAtLevel(level)
let url = this._url
.replace('{x}', String(x))
.replace('{y}', String(-y))
.replace('{z}', level)
.replace('{s}', String(1))
.replace('{style}', this._style)
.replace('{labelStyle}', this._labelStyle)
.replace('{time}', String(new Date().getTime()))

if (this._crs === 'WGS84') {
url = url.replace('{x}', String(x)).replace('{y}', String(-y))
} else {
url = url
.replace('{x}', String(x - xTiles / 2))
.replace('{y}', String(yTiles / 2 - y - 1))
}
return Cesium.ImageryProvider.loadImage(this, url)
}
}

+ 7
- 20
src/core/imagery/tiling-scheme/BaiduMercatorTilingScheme.js 查看文件

@@ -14,18 +14,11 @@ class BaiduMercatorTilingScheme extends Cesium.WebMercatorTilingScheme {
let projection = new BaiduMercatorProjection()
this._projection.project = function(cartographic, result) {
result = result || {}
if (options?.crs === 'WGS84') {
result = CoordTransform.WGS84ToGCJ02(
Cesium.Math.toDegrees(cartographic.longitude),
Cesium.Math.toDegrees(cartographic.latitude)
)
result = CoordTransform.GCJ02ToBD09(result[0], result[1])
} else {
result = CoordTransform.GCJ02ToBD09(
Cesium.Math.toDegrees(cartographic.longitude),
Cesium.Math.toDegrees(cartographic.latitude)
)
}
result = CoordTransform.WGS84ToGCJ02(
Cesium.Math.toDegrees(cartographic.longitude),
Cesium.Math.toDegrees(cartographic.latitude)
)
result = CoordTransform.GCJ02ToBD09(result[0], result[1])
result[0] = Math.min(result[0], 180)
result[0] = Math.max(result[0], -180)
result[1] = Math.min(result[1], 74.000022)
@@ -36,20 +29,14 @@ class BaiduMercatorTilingScheme extends Cesium.WebMercatorTilingScheme {
})
return new Cesium.Cartesian2(result.x, result.y)
}

this._projection.unproject = function(cartesian, result) {
result = result || {}
result = projection.mercatorToLngLat({
lng: cartesian.x,
lat: cartesian.y
})
result[0] = ((result[0] + 180) % 360) - 180
if (options?.crs === 'WGS84') {
result = CoordTransform.BD09ToGCJ02(result.lng, result.lat)
result = CoordTransform.GCJ02ToWGS84(result[0], result[1])
} else {
result = CoordTransform.BD09ToGCJ02(result.lng, result.lat)
}
result = CoordTransform.BD09ToGCJ02(result.lng, result.lat)
result = CoordTransform.GCJ02ToWGS84(result[0], result[1])
return new Cesium.Cartographic(
Cesium.Math.toRadians(result[0]),
Cesium.Math.toRadians(result[1])

Loading…
取消
儲存