From e82825c9fca1b3357b76b6e615b91dd14e96fa2c Mon Sep 17 00:00:00 2001 From: Cmen <1176967590@qq.com> Date: Fri, 11 Mar 2022 08:33:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=85=A5geojson=E5=B9=B6=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=E5=9D=90=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store/utils/getGeoJSON.ts | 2 +- src/store/utils/getMapOptions.ts | 59 +++++++++++++++++++++++++++++--- src/utils/covert.d.ts | 7 +++- 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/src/store/utils/getGeoJSON.ts b/src/store/utils/getGeoJSON.ts index bad2d7d..03b4e69 100644 --- a/src/store/utils/getGeoJSON.ts +++ b/src/store/utils/getGeoJSON.ts @@ -64,7 +64,7 @@ export function getGeoJSON(state: IMapOptions): GeoJSON.FeatureCollection { overlays .filter((overlay) => overlay.type === OverlayTypes.Polyline) .forEach((polyline) => { - const path = polyline.path!; + const path = polyline.path!.map((pos) => [...pos]); const properties: GeoJSON.GeoJsonProperties = { fineType: OverlayTypes.Polyline, }; diff --git a/src/store/utils/getMapOptions.ts b/src/store/utils/getMapOptions.ts index 445b711..2bc6975 100644 --- a/src/store/utils/getMapOptions.ts +++ b/src/store/utils/getMapOptions.ts @@ -1,18 +1,32 @@ -import { IMapOptions, IOverlay } from "@map"; +import { IMapOptions, IOverlay, OverlayCategory } from "@map"; import { OverlayTypes } from "@types"; -import { getUID } from "@utils"; +import { getUID, gps84_To_gcj02 } from "@utils"; export function getMapOptions(geojson: GeoJSON.FeatureCollection) { const { features } = geojson; const mapOptions: IMapOptions = { overlays: [], }; + let maxLat = -90, + minLat = 90, + maxLng = -180, + minLng = 180; + + const updateBounds = (path: GeoJSON.Position[]) => { + path.forEach(([lng, lat]) => { + minLat = Math.min(lat, minLat); + maxLat = Math.max(lat, maxLat); + minLng = Math.min(lng, minLng); + maxLng = Math.max(lng, maxLng); + }); + }; const addPolygon = (feature: GeoJSON.Feature) => { const { geometry, properties } = feature; - const { name, fineType, lngLat, radius } = properties as any; + const { name, fineType, lngLat, radius, building, water, grass, height } = + properties as any; const { coordinates } = geometry as GeoJSON.Polygon; - const [path] = coordinates; + const path = convertPath(coordinates[0]); const overlay: IOverlay = { id: getUID(), name, @@ -29,11 +43,34 @@ export function getMapOptions(geojson: GeoJSON.FeatureCollection) { overlay.type = OverlayTypes.Polygon; overlay.path = path; } + + updateBounds(path); + + overlay.category = building + ? OverlayCategory.Building + : grass + ? OverlayCategory.Grass + : water + ? OverlayCategory.Water + : undefined; + overlay.height = height; + mapOptions.overlays.push(overlay); }; const addPolyline = (feature: GeoJSON.Feature) => { - // + const { geometry, properties } = feature; + const { name, road } = properties as any; + const { coordinates } = geometry as GeoJSON.LineString; + const overlay: IOverlay = { + id: getUID(), + name, + type: OverlayTypes.Polyline, + category: road ? OverlayCategory.Road : undefined, + path: convertPath(coordinates), + }; + mapOptions.overlays.push(overlay); + updateBounds(coordinates); }; features.forEach((feature) => { @@ -46,5 +83,17 @@ export function getMapOptions(geojson: GeoJSON.FeatureCollection) { } }); + mapOptions.center = [(maxLng + minLng) / 2, (maxLat + minLat) / 2]; + // 先写个16吧, 懒得算了. + mapOptions.zoom = 16; + return mapOptions; } + +function convertPath(path: GeoJSON.Position[]) { + return path.map((pos) => { + let [lng, lat] = pos; + ({ lng, lat } = gps84_To_gcj02(lng, lat)); + return [lng, lat]; + }); +} diff --git a/src/utils/covert.d.ts b/src/utils/covert.d.ts index 7498ca7..3fb96bf 100644 --- a/src/utils/covert.d.ts +++ b/src/utils/covert.d.ts @@ -3,4 +3,9 @@ declare function gcj02_To_gps84( lat: number ): { lng: number; lat: number }; -export { gcj02_To_gps84 }; +declare function gps84_To_gcj02( + lng: number, + lat: number +): { lng: number; lat: number }; + +export { gcj02_To_gps84, gps84_To_gcj02 };