Skip to content

Commit

Permalink
refactor(algo): cleanup utils (#630)
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb authored May 23, 2023
1 parent a2e5d52 commit ab713ad
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
16 changes: 6 additions & 10 deletions src/algorithms/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ describe("distanceBetweenPoints", () => {

describe("extendPixelBounds", () => {
test("is correct", () => {
const northEast = { x: 0, y: 0 } as google.maps.Point;
const southWest = { x: 0, y: 0 } as google.maps.Point;
expect(extendPixelBounds({ northEast, southWest }, 1)).toEqual({
const northEast = new google.maps.Point(0, 0);
const southWest = new google.maps.Point(0, 0);
expect(extendPixelBounds({ northEast, southWest }, 1)).toMatchObject({
northEast: {
x: 1,
y: -1,
Expand All @@ -64,15 +64,11 @@ describe("extendPixelBounds", () => {

describe("pixelBoundsToLatLngBounds", () => {
test("is correct", () => {
const northEast = { x: 1, y: 1 } as google.maps.Point;
const southWest = { x: -1, y: -1 } as google.maps.Point;
const northEast = new google.maps.Point(1, 1);
const southWest = new google.maps.Point(-1, -1);
const projection = new MapCanvasProjection();
const bounds = pixelBoundsToLatLngBounds(
{ northEast, southWest },
projection
);
pixelBoundsToLatLngBounds({ northEast, southWest }, projection);
expect(projection.fromDivPixelToLatLng).toHaveBeenCalledWith(northEast);
expect(projection.fromDivPixelToLatLng).toHaveBeenCalledWith(southWest);
expect(bounds.extend).toHaveBeenCalledTimes(2);
});
});
42 changes: 28 additions & 14 deletions src/algorithms/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,53 @@

import { MarkerUtils } from "../marker-utils";

/**
* Returns the markers visible in a padded map viewport
*
* @param map
* @param mapCanvasProjection
* @param markers The list of marker to filter
* @param viewportPaddingPixels The padding in pixel
* @returns The list of markers in the padded viewport
*/
export const filterMarkersToPaddedViewport = (
map: google.maps.Map,
mapCanvasProjection: google.maps.MapCanvasProjection,
markers: Marker[],
viewportPadding: number
viewportPaddingPixels: number
): Marker[] => {
const extendedMapBounds = extendBoundsToPaddedViewport(
map.getBounds(),
mapCanvasProjection,
viewportPadding
viewportPaddingPixels
);
return markers.filter((marker) =>
extendedMapBounds.contains(MarkerUtils.getPosition(marker))
);
};

/**
* Extends a bounds by a number of pixels in each direction.
* Extends a bounds by a number of pixels in each direction
*/
export const extendBoundsToPaddedViewport = (
bounds: google.maps.LatLngBounds,
projection: google.maps.MapCanvasProjection,
pixels: number
numPixels: number
): google.maps.LatLngBounds => {
const { northEast, southWest } = latLngBoundsToPixelBounds(
bounds,
projection
);
const extendedPixelBounds = extendPixelBounds(
{ northEast, southWest },
pixels
numPixels
);
return pixelBoundsToLatLngBounds(extendedPixelBounds, projection);
};

/**
* Returns the distance between 2 positions.
*
* @hidden
*/
export const distanceBetweenPoints = (
Expand All @@ -77,6 +88,8 @@ type PixelBounds = {
};

/**
* Converts a LatLng bound to pixels.
*
* @hidden
*/
const latLngBoundsToPixelBounds = (
Expand All @@ -90,17 +103,19 @@ const latLngBoundsToPixelBounds = (
};

/**
* Extends a pixel bounds by numPixels in all directions.
*
* @hidden
*/
export const extendPixelBounds = (
{ northEast, southWest }: PixelBounds,
pixels: number
numPixels: number
): PixelBounds => {
northEast.x += pixels;
northEast.y -= pixels;
northEast.x += numPixels;
northEast.y -= numPixels;

southWest.x -= pixels;
southWest.y += pixels;
southWest.x -= numPixels;
southWest.y += numPixels;

return { northEast, southWest };
};
Expand All @@ -112,8 +127,7 @@ export const pixelBoundsToLatLngBounds = (
{ northEast, southWest }: PixelBounds,
projection: google.maps.MapCanvasProjection
): google.maps.LatLngBounds => {
const bounds = new google.maps.LatLngBounds();
bounds.extend(projection.fromDivPixelToLatLng(northEast));
bounds.extend(projection.fromDivPixelToLatLng(southWest));
return bounds;
const sw = projection.fromDivPixelToLatLng(southWest);
const ne = projection.fromDivPixelToLatLng(northEast);
return new google.maps.LatLngBounds(sw, ne);
};

0 comments on commit ab713ad

Please sign in to comment.