Overlapping Marker Spiderfier + MarkerWithLabel

1.9k Views Asked by At

Using the Google Maps JavaScript API v3 with Overlapping Marker Spiderfier and MarkerWithLabel has an adverse effect when spiderfying: all markers will spiderfy and then immediately return to their original position (not via unspiderfy) except for the first marker.

That is, the Marker event position_changed is triggered an additional n - 1 times after spiderfying, where n is the number of markers spiderfied.

enter image description here

1

There are 1 best solutions below

0
On

The issue stems from the optimized Marker attribute, which MarkerWithLabel forces to false. Leaving optimized as true (the default setting) should result in correct spiderfying. Doing so requires a patch to the MarkerWithLabel source though: removing the lines that set optimized = false:

function MarkerWithLabel(opt_options) {
  opt_options = opt_options || {};
  opt_options.labelContent = opt_options.labelContent || "";
  opt_options.labelAnchor = opt_options.labelAnchor || new google.maps.Point(0, 0);
  opt_options.labelClass = opt_options.labelClass || "markerLabels";
  opt_options.labelStyle = opt_options.labelStyle || {};
  opt_options.labelInBackground = opt_options.labelInBackground || false;
  if (typeof opt_options.labelVisible === "undefined") {
    opt_options.labelVisible = true;
  }
  if (typeof opt_options.raiseOnDrag === "undefined") {
    opt_options.raiseOnDrag = true;
  }
  if (typeof opt_options.clickable === "undefined") {
    opt_options.clickable = true;
  }
  if (typeof opt_options.draggable === "undefined") {
    opt_options.draggable = false;
  }
  // if (typeof opt_options.optimized === "undefined") {
  //   opt_options.optimized = false;
  // }
  opt_options.crossImage = opt_options.crossImage || "http" + (document.location.protocol === "https:" ? "s" : "") + "://maps.gstatic.com/intl/en_us/mapfiles/drag_cross_67_16.png";
  opt_options.handCursor = opt_options.handCursor || "http" + (document.location.protocol === "https:" ? "s" : "") + "://maps.gstatic.com/intl/en_us/mapfiles/closedhand_8_8.cur";
  // opt_options.optimized = false; // Optimized rendering is not supported

  this.label = new MarkerLabel_(this, opt_options.crossImage, opt_options.handCursor); // Bind the label to the marker

  // Call the parent constructor. It calls Marker.setValues to initialize, so all
  // the new parameters are conveniently saved and can be accessed with get/set.
  // Marker.set triggers a property changed event (called "propertyname_changed")
  // that the marker label listens for in order to react to state changes.
  google.maps.Marker.apply(this, arguments);
}

enter image description here