使用重叠标记 Spiderfier 时获取地图标记的新位置

Get new position of map marker when using Overlapping Marker Spiderfier

我有一张带有标记的 google 地图,它使用 OverlappingMarkerSpiderfier 来散布非常紧密重叠的标记。我需要检索鼠标事件标记的屏幕位置(在真实版本中,我制作的工具提示过于复杂,无法融入 google 提供的默认设置)。

function project(latLng) {
  var TILE_SIZE = 256;
  var siny = Math.sin(latLng.lat * Math.PI / 180);

  siny = Math.min(Math.max(siny, -0.9999), 0.9999);

  return new google.maps.Point(
      TILE_SIZE * (0.5 + latLng.lng / 360),
      TILE_SIZE * (0.5 - Math.log((1 + siny) / (1 - siny)) / (4 * Math.PI)));
}

function getPixelPosition(marker, map) {
  var zoom = map.getZoom();
  var scale = 1 << zoom;
  var loc = marker.data.location;
  var worldCoordinate = project(loc);
  var pixelCoordinate = new google.maps.Point(
      Math.floor(worldCoordinate.x * scale),
      Math.floor(worldCoordinate.y * scale));

  return pixelCoordinate;
}

function getMapTopLeftPosition(map) {
  var zoom = map.getZoom();
  var scale = 1 << zoom;
  var topLeft = new google.maps.LatLng(
        map.getBounds().getNorthEast().lat(),
        map.getBounds().getSouthWest().lng());
  var projection = map.getProjection();
  var topLeftWorldCoordinate = projection.fromLatLngToPoint(topLeft);
  var topLeftPixelCoordinate = new google.maps.Point(
          topLeftWorldCoordinate.x * scale,
          topLeftWorldCoordinate.y * scale);

  return topLeftPixelCoordinate;
}

function getMarkerRelativePosition(marker, map) {
  const screenPosition = getPixelPosition(marker, map);

  const topLeftMapPosition = getMapTopLeftPosition(map);

  const markerHeight = 60;
  const markerWidth = 48;
  return {
    x: screenPosition.x - topLeftMapPosition.x - (markerWidth / 2),
    y: screenPosition.y - topLeftMapPosition.y - markerHeight,
  };
}

function showInfo(position, text) {
  // triggered on marker mouseover
  const mapContainer = document.getElementById('map-container');
  const infoDiv = document.createElement('div');
  infoDiv.innerHTML = text;
  infoDiv.setAttribute('id', 'info')
  infoDiv.style.left = position.x + 'px';
  infoDiv.style.top = position.y + 'px';
  mapContainer.appendChild(infoDiv);
}

function hideInfo() {
  // triggered on marker mouseout
  const info = document.getElementById('info')
  info.remove();
}

对于尚未通过 OMS 从群集中散布的标记,这可以按我的要求工作。然而,当我点击一组标记来展开它们时,上面的代码仍然是 returns 标记的原始位置。请参阅此处的工作示例:https://jsfiddle.net/ebbishop/s2mzgp0b/

我怀疑有一种方法可以使用 oms 库获取标记的新位置,但我无法在文档中找到它。

如何检索标记的新位置或 x 和 y 坐标的变化?

问题出在函数 getPixelPosition 上。我们可以很容易地改变这一行

var loc = marker.data.location,其中使用了一个自定义的,static数据属性集用于其他目的,并将其替换为

var loc = { lat: marker.getPosition().lat(), lng: marker.getPosition().lng() },获取标记的当前位置。

所有 oms 所做的,经过大量计算后,就是使用 gmaps 自己的 setPosition 方法更新 google 地图标记的位置,所以这工作得很好。

Updated, working example