Skip to content

Commit

Permalink
chore: split depends on @antv/l7-map
Browse files Browse the repository at this point in the history
  • Loading branch information
lvisei committed May 27, 2024
1 parent afaa51a commit 5e0a102
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
10 changes: 5 additions & 5 deletions packages/extension-maps/src/gmap/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import type {
Point,
} from '@antv/l7-core';
import { BaseMapService, MapServiceEvent, WebMercatorViewport } from '@antv/l7-core';
import { MercatorCoordinate } from '@antv/l7-map';
import { DOM } from '@antv/l7-utils';
import { mat4, vec3 } from 'gl-matrix';
import { lngLatToMercator } from '../utils';
import './logo.css';
import GMapLoader from './maploader';

Expand Down Expand Up @@ -389,9 +389,9 @@ export default class GMapService extends BaseMapService<google.maps.Map> {
new google.maps.LatLng(outerLat, outerLon),
]);

const [x1, y1] = this.lngLatToCoord!([centerLon, centerLat]);
const [x2, y2] = this.lngLatToCoord!([outerLon, outerLat]);
const coordDistance = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
const { x: x1, y: y1 } = this.lngLatToPixel([centerLon, centerLat]);
const { x: x2, y: y2 } = this.lngLatToPixel([outerLon, outerLat]);
const coordDistance = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y2 - y1, 2));

return coordDistance / metreDistance;
}
Expand Down Expand Up @@ -429,7 +429,7 @@ export default class GMapService extends BaseMapService<google.maps.Map> {

public lngLatToMercator(lnglat: [number, number], altitude: number): IMercator {
// Use built in mercator tools due to Tencent not provided related methods
const { x = 0, y = 0, z = 0 } = MercatorCoordinate.fromLngLat(lnglat, altitude);
const { x = 0, y = 0, z = 0 } = lngLatToMercator(lnglat, altitude);
return { x, y, z };
}

Expand Down
4 changes: 2 additions & 2 deletions packages/extension-maps/src/tdtmap/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
Point,
} from '@antv/l7-core';
import { BaseMapService, MapServiceEvent, WebMercatorViewport } from '@antv/l7-core';
import { MercatorCoordinate } from '@antv/l7-map';
import { lngLatToMercator } from '../utils';
import { load } from './maploader';

const MapEvent: Record<string, string[] | string> = {
Expand Down Expand Up @@ -478,7 +478,7 @@ export default class TdtMapService extends BaseMapService<any> {

public lngLatToMercator(lnglat: [number, number], altitude: number): IMercator {
// Use built in mercator tools due to Tencent not provided related methods
const { x = 0, y = 0, z = 0 } = MercatorCoordinate.fromLngLat(lnglat, altitude);
const { x = 0, y = 0, z = 0 } = lngLatToMercator(lnglat, altitude);
return { x, y, z };
}

Expand Down
4 changes: 2 additions & 2 deletions packages/extension-maps/src/tmap/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import type {
Point,
} from '@antv/l7-core';
import { BaseMapService, MapServiceEvent, WebMercatorViewport } from '@antv/l7-core';
import { MercatorCoordinate } from '@antv/l7-map';
import { DOM } from '@antv/l7-utils';
import { mat4, vec3 } from 'gl-matrix';
import { lngLatToMercator } from '../utils';
import './logo.css';
import TMapLoader from './maploader';

Expand Down Expand Up @@ -437,7 +437,7 @@ export default class TencentMapService extends BaseMapService<TMap.Map> {

public lngLatToMercator(lnglat: [number, number], altitude: number): IMercator {
// Use built in mercator tools due to Tencent not provided related methods
const { x = 0, y = 0, z = 0 } = MercatorCoordinate.fromLngLat(lnglat, altitude);
const { x = 0, y = 0, z = 0 } = lngLatToMercator(lnglat, altitude);
return { x, y, z };
}

Expand Down
32 changes: 32 additions & 0 deletions packages/extension-maps/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,35 @@ export function toPaddingOptions(padding: Padding = {}) {

return { ...defaultPadding, ...padding };
}

export function lngLatToMercator(lnglat: [number, number], altitude: number) {
const mercatorXfromLng = (lng: number) => {
return (180 + lng) / 360;
};

const mercatorYfromLat = (lat: number) => {
return (180 - (180 / Math.PI) * Math.log(Math.tan(Math.PI / 4 + (lat * Math.PI) / 360))) / 360;
};

const mercatorZfromAltitude = (altitude: number, lat: number) => {
/*
* The circumference at a line of latitude in meters.
*/
const circumferenceAtLatitude = (latitude: number) => {
const earthRadius = 6371008.8;
/*
* The average circumference of the world in meters.
*/
const earthCircumfrence = 2 * Math.PI * earthRadius; // meters
return earthCircumfrence * Math.cos((latitude * Math.PI) / 180);
};

return altitude / circumferenceAtLatitude(lat);
};

const x = mercatorXfromLng(lnglat[0]);
const y = mercatorYfromLat(lnglat[1]);
const z = mercatorZfromAltitude(altitude, lnglat[1]);

return { x, y, z };
}

0 comments on commit 5e0a102

Please sign in to comment.