Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not work with collisionBehavior on vector maps #493

Open
Mobiletainment opened this issue Apr 9, 2022 · 2 comments
Open

Does not work with collisionBehavior on vector maps #493

Mobiletainment opened this issue Apr 9, 2022 · 2 comments
Labels
triage me I really want to be triaged. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@Mobiletainment
Copy link

Thank you for the library.
I'm currently using the vector maps with the new Marker Collision Management which is currently in beta.

My use case is that I have many markers and I'm using the collision behavior google.maps.CollisionBehavior.OPTIONAL_AND_HIDES_LOWER_PRIORITY to hide overlapped markers with lower priority.

This works well for markers constructed as google.maps.Marker without any labels. As soon as I start to use labels with MarkerWithLabel, I'm seeing leftover labels for markers which are hidden.

The following example shows the collision behavior where only 2 markers remain visible while 11 markers should be hidden:

  • google.maps.Marker:
    image

  • MarkerWithLabel:
    image

Environment details

  1. Specify the API at the beginning of the title (for example, "Places: ...")
  • Maps JavaScript API beta channel (v=beta)
  • Map type: vector map
  1. OS type and version
  • all
  1. Library version and other environment information
  • @googlemaps/markerwithlabel": "2.0.9

Steps to reproduce

Please check out the beta documentation for Marker Collision Management which is used as a base for this example.

Code example

<!-- load map using the beta channel -->
<script
      src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&libraries=&v=beta"
      defer
></script>

// add 13 markers to the map and apply collision behavior
function initMap(): void {
  const map = new google.maps.Map(
    document.getElementById("map") as HTMLElement,
    {
      mapId: "3a3b33f0edd6ed2a", // important: this needs to refer to a vector map for JavaScript
      center: { lat: 47.609414458375674, lng: -122.33897030353548 },
      zoom: 17,
    } as google.maps.MapOptions
  );

  [
    [-122.3402, 47.6093],
    [-122.3402, 47.6094],
    [-122.3403, 47.6094],
    [-122.3384, 47.6098],
    [-122.3389, 47.6095],
    [-122.3396, 47.6095],
    [-122.3379, 47.6097],
    [-122.3378, 47.6097],
    [-122.3396, 47.6091],
    [-122.3383, 47.6089],
    [-122.3379, 47.6093],
    [-122.3381, 47.6095],
    [-122.3378, 47.6095],
  ].map(
    ([lng, lat]: number[], i: number) =>
      new MarkerWithLabel({
        position: new google.maps.LatLng({ lat, lng }),
        map,
        zIndex: i, // assign zIndex which is used as the priority for the marker (higher zIndex = higher priority)
        labelContent: `Marker: ${i + 1}`, // provide label to the marker
        collisionBehavior:
          google.maps.CollisionBehavior.OPTIONAL_AND_HIDES_LOWER_PRIORITY, // don't show marker if overlapped by higher prio
      })
  );

Thank you for your support.

@Mobiletainment Mobiletainment added triage me I really want to be triaged. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Apr 9, 2022
@jpoehnelt
Copy link
Contributor

@Mobiletainment Thank you for opening this issue. 🙏
Please check out these other resources that might be applicable:

This is an automated message, feel free to ignore.

@Mobiletainment
Copy link
Author

I tried to inspect how the collision behavior works, but neither of these approaches was successful:

  • act on zoom changed: since the collisions only change when the zoom has changed, I thought it would be possible to determine which ones are still visible, but apparently the collision behavior doesn't set them to invisible (which may have solved the problem in the first place I guess):
map.addListener("zoom_changed", (e) => {
  console.log(markers.filter((m) => m.getVisible())); // it still tells me that all markers are visible, despite some of them not being shown
});
  • listen if any events on the marker were fired, e.g.:
markers.forEach((marker) => {
    marker.addListener("shape_changed", (e) => {
      console.log("event fired", marker); // never called
    });
  });

It would be really nice if google maps exposed an event for changes in the collisions.

@jpoehnelt jpoehnelt removed their assignment Jun 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage me I really want to be triaged. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

2 participants