Skip to content

Commit d1f499e

Browse files
authored
fix(static-maps): remove path grouping by style (#809)
1 parent bed6ccd commit d1f499e

File tree

2 files changed

+77
-51
lines changed

2 files changed

+77
-51
lines changed

src/libraries/__tests__/create-static-maps-url.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,35 @@ describe('createStaticMapsUrl', () => {
203203
expect(url).toContain(`path=${encodedPath2}`);
204204
});
205205

206+
test('handles multiple paths with same styles', () => {
207+
const url = createStaticMapsUrl({
208+
...requiredParams,
209+
paths: [
210+
{
211+
coordinates: [{lat: 40.737102, lng: -73.990318}, 'Hamburg, Germany'],
212+
color: 'red',
213+
weight: 5
214+
},
215+
{
216+
coordinates: [
217+
{lat: 40.737102, lng: -73.990318},
218+
{lat: 40.736102, lng: -73.989318}
219+
],
220+
color: 'red',
221+
weight: 5
222+
}
223+
]
224+
});
225+
const encodedPath1 = encodeURIComponent(
226+
'color:red|weight:5|40.737102,-73.990318|Hamburg, Germany'
227+
).replace(/%20/g, '+');
228+
const encodedPath2 = encodeURIComponent(
229+
'color:red|weight:5|40.737102,-73.990318|40.736102,-73.989318'
230+
);
231+
expect(url).toContain(`path=${encodedPath1}`);
232+
expect(url).toContain(`path=${encodedPath2}`);
233+
});
234+
206235
test('includes style parameters', () => {
207236
const url = createStaticMapsUrl({
208237
...requiredParams,
Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,52 @@
11
import {formatLocation, formatParam} from './helpers';
22
import {StaticMapsPath} from './types';
33

4+
// Style properties that can be applied to paths in the Static Maps API
5+
const PATH_STYLE_KEYS = ['color', 'weight', 'fillcolor', 'geodesic'] as const;
6+
47
/**
5-
* Assembles path parameters for the Static Maps Api from an array of paths.
8+
* Builds the style portion of a path parameter string.
9+
* @param path - The path object containing style properties
10+
* @returns A string with style parameters in the format "|key:value"
11+
*/
12+
function buildStyleParams(path: StaticMapsPath): string {
13+
let styleParams = '';
14+
15+
PATH_STYLE_KEYS.forEach(key => {
16+
if (path[key] !== undefined) {
17+
styleParams += `|${key}:${path[key]}`;
18+
}
19+
});
20+
21+
return styleParams;
22+
}
23+
24+
/**
25+
* Builds the coordinates portion of a path parameter string.
26+
* @param coordinates - Either a string or array of location objects
27+
* @returns A string with coordinates in the format "|lat,lng|lat,lng"
28+
*/
29+
function buildCoordinateParams(
30+
coordinates: StaticMapsPath['coordinates']
31+
): string {
32+
if (typeof coordinates === 'string') {
33+
return `|${decodeURIComponent(coordinates)}`;
34+
}
35+
36+
return coordinates.map(location => `|${formatLocation(location)}`).join('');
37+
}
38+
39+
/**
40+
* Assembles path parameters for the Static Maps API from an array of paths.
641
*
7-
* This function groups paths by their style properties (color, weight, fillcolor, geodesic)
8-
* and then constructs a string of path parameters for each group. Each path parameter string
42+
* This function constructs a string of path parameters for each path. Each path parameter string
943
* includes the style properties and the coordinates of the paths.
1044
*
1145
* @param {Array<StaticMapsPath>} [paths=[]] - An array of paths to be assembled into path parameters.
1246
* @returns {Array<string>} An array of path parameter strings.
1347
*
1448
* @example
49+
* ```typescript
1550
* const paths = [
1651
* {
1752
* color: 'red',
@@ -24,56 +59,18 @@ import {StaticMapsPath} from './types';
2459
* ];
2560
*
2661
* const pathParams = assemblePathParams(paths);
27-
* Output: [
28-
* 'color:red|weight:5|40.714728,-73.998672|40.718217,-73.998284'
29-
* ]
62+
* // Output: ['color:red|weight:5|40.714728,-73.998672|40.718217,-73.998284']
63+
* ```
3064
*/
31-
export function assemblePathParams(paths: Array<StaticMapsPath> = []) {
32-
const pathParams: Array<string> = [];
33-
34-
// Group paths by their style properties (color, weight, fillcolor, geodesic)
35-
// to combine paths with identical styles into single parameter strings
36-
const pathsByStyle = paths?.reduce(
37-
(styles, path) => {
38-
const {color = 'default', weight, fillcolor, geodesic} = path;
39-
40-
// Create unique key for this style combination
41-
const key = [color, weight, fillcolor, geodesic]
42-
.filter(Boolean)
43-
.join('-');
65+
export function assemblePathParams(
66+
paths: Array<StaticMapsPath> = []
67+
): string[] {
68+
return paths.map(path => {
69+
const styleParams = buildStyleParams(path);
70+
const coordinateParams = buildCoordinateParams(path.coordinates);
4471

45-
styles[key] = styles[key] || [];
46-
styles[key].push(path);
47-
return styles;
48-
},
49-
{} as Record<string, Array<StaticMapsPath>>
50-
);
72+
const pathParam = styleParams + coordinateParams;
5173

52-
// Process each group of paths with identical styles
53-
Object.values(pathsByStyle ?? {}).forEach(paths => {
54-
let pathParam = '';
55-
56-
// Build style parameter string using properties from first path in group
57-
// since all paths in this group share the same style
58-
Object.entries(paths[0]).forEach(([key, value]) => {
59-
if (['color', 'weight', 'fillcolor', 'geodesic'].includes(key)) {
60-
pathParam += `|${key}:${value}`;
61-
}
62-
});
63-
64-
// Add location for all marker in style group
65-
for (const path of paths) {
66-
if (typeof path.coordinates === 'string') {
67-
pathParam += `|${decodeURIComponent(path.coordinates)}`;
68-
} else {
69-
for (const location of path.coordinates) {
70-
pathParam += `|${formatLocation(location)}`;
71-
}
72-
}
73-
}
74-
75-
pathParams.push(pathParam);
74+
return formatParam(pathParam);
7675
});
77-
78-
return pathParams.map(formatParam);
7976
}

0 commit comments

Comments
 (0)