react-google-maps icon indicating copy to clipboard operation
react-google-maps copied to clipboard

[Bug] Static Map merges coordinate paths with the same style

Open Mauriciocr207 opened this issue 1 year ago • 3 comments

Description

I expected that each path passed in the paths array in the path property of the createStaticMapsUrl function would be drawn independently. Instead, the paths are grouped by their style. When multiple paths share the same style, their coordinate arrays are merged into a single path.

I checked the implementation in the file:

src/libraries/create-static-maps-url/assemble-path-params.ts

And it seems that grouping paths by style is the intended behavior, as seen in this function:

src/libraries/create-static-maps-url/assemble-path-params.ts:

import {formatLocation, formatParam} from './helpers';
import {StaticMapsPath} from './types';

/**
 * Assembles path parameters for the Static Maps Api from an array of paths.
 *
 * This function groups paths by their style properties (color, weight, fillcolor, geodesic)
 * and then constructs a string of path parameters for each group. Each path parameter string
 * includes the style properties and the coordinates of the paths.
 *
 * @param {Array<StaticMapsPath>} [paths=[]] - An array of paths to be assembled into path parameters.
 * @returns {Array<string>} An array of path parameter strings.
 *
 * @example
 * const paths = [
 *   {
 *     color: 'red',
 *     weight: 5,
 *     coordinates: [
 *       { lat: 40.714728, lng: -73.998672 },
 *       { lat: 40.718217, lng: -73.998284 }
 *     ]
 *   }
 * ];
 *
 * const pathParams = assemblePathParams(paths);
 * Output: [
 *    'color:red|weight:5|40.714728,-73.998672|40.718217,-73.998284'
 *  ]
 */
export function assemblePathParams(paths: Array<StaticMapsPath> = []) {
  const pathParams: Array<string> = [];

  // Group paths by their style properties (color, weight, fillcolor, geodesic)
  // to combine paths with identical styles into single parameter strings
  const pathsByStyle = paths?.reduce(
    (styles, path) => {
      const {color = 'default', weight, fillcolor, geodesic} = path;

      // Create unique key for this style combination
      const key = [color, weight, fillcolor, geodesic]
        .filter(Boolean)
        .join('-');

      styles[key] = styles[key] || [];
      styles[key].push(path);
      return styles;
    },
    {} as Record<string, Array<StaticMapsPath>>
  );

  // Process each group of paths with identical styles
  Object.values(pathsByStyle ?? {}).forEach(paths => {
    let pathParam = '';

    // Build style parameter string using properties from first path in group
    // since all paths in this group share the same style
    Object.entries(paths[0]).forEach(([key, value]) => {
      if (['color', 'weight', 'fillcolor', 'geodesic'].includes(key)) {
        pathParam += `|${key}:${value}`;
      }
    });

    // Add location for all marker in style group
    for (const path of paths) {
      if (typeof path.coordinates === 'string') {
        pathParam += `|${decodeURIComponent(path.coordinates)}`;
      } else {
        for (const location of path.coordinates) {
          pathParam += `|${formatLocation(location)}`;
        }
      }
    }

    pathParams.push(pathParam);
  });

  return pathParams.map(formatParam);
}

However, this causes a problem in my case:

I have an array of paths where many of them share the same style (starting from the second element):

Path array: Image

Static map: Image

As you can see, I expected the green rectangles not to be joined together, but since they share the same style, their coordinates were merged into a single path.

Also, here is the full static URL showing the current behavior, only two paths are rendered because they share the same style, but I need them to be drawn separately, even if their styles are identical.

Image

Steps to Reproduce

To reproduce just pass the following static maps url props to the createStaticMapsUrl() function

const staticMapsUrl = createStaticMapsUrl({
    apiKey: '...',
    width: 565,
    height: 300,
    paths: [
      {
        weight: 2,
        color: '0x000000',
        fillcolor: '0x008000',
        coordinates: [
          {
            lat: 43.00351033557169,
            lng: 6.200336017066529,
          },
          {
            lat: 43.00350692079141,
            lng: 6.200335701382353,
          },
          {
            lat: 43.00350678128206,
            lng: 6.200337691242284,
          },
          {
            lat: 43.00350676738746,
            lng: 6.200337969228981,
          },
          {
            lat: 43.00350671299235,
            lng: 6.200339904053872,
          },
          {
            lat: 43.00351012777156,
            lng: 6.200340219738048,
          },
          {
            lat: 43.00351033557169,
            lng: 6.200336017066529,
          },
        ],
      },
      {
        weight: 2,
        color: '0x000000',
        fillcolor: '0x008000',
        coordinates: [
          {
            lat: 43.00351007786288,
            lng: 6.200271854105546,
          },
          {
            lat: 43.00351349264263,
            lng: 6.200272169813151,
          },
          {
            lat: 43.00351370044276,
            lng: 6.200267967141632,
          },
          {
            lat: 43.00351028566301,
            lng: 6.200267651434027,
          },
          {
            lat: 43.00351014615367,
            lng: 6.200269641317388,
          },
          {
            lat: 43.00351013225906,
            lng: 6.200269919280655,
          },
          {
            lat: 43.00351007786288,
            lng: 6.200271854105546,
          },
        ],
      },
    ],
    zoom: 21,
  })

Environment

  • Library version: ^1.5.2
  • Google maps version: "3.61.12c"
  • Browser and Version: Mozilla Firefox 141.0 (64-bit)
  • OS: Windows

Logs


Mauriciocr207 avatar Aug 01 '25 19:08 Mauriciocr207

Hi @Mauriciocr207 :)

Thanks for taking the time to report the bug! Also great job providing all the info necessary to quickly see whats going on and to reproduce the issue.

Bug is valid, grouping by style does not make sense here. Fix is on the way #809

mrMetalWood avatar Aug 07 '25 07:08 mrMetalWood

Is being released with the 1.5.5 version, should already be published when you read this.

usefulthink avatar Aug 07 '25 08:08 usefulthink

Excelent, @mrMetalWood, @usefulthink ! Now my maps looks great!

Image

Mauriciocr207 avatar Aug 07 '25 15:08 Mauriciocr207