简体   繁体   English

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

[英]Get new position of map marker when using Overlapping Marker Spiderfier

I have a google map with markers that uses OverlappingMarkerSpiderfier to spread markers that overlap very closely.我有一个带有标记的谷歌地图,它使用OverlappingMarkerSpiderfier来传播非常紧密重叠的标记。 I have a need to retrieve the screen position of markers on mouse events (in the real version, I'm making tooltips that are much too complex to wedge into the default that google offers).我需要在鼠标事件上检索标记的屏幕位置(在真实版本中,我制作的工具提示太复杂了,无法融入谷歌提供的默认值)。

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();
}

This works as I want for markers that haven't been spread out from a cluster by OMS.对于尚未由 OMS 从集群中展开的标记,这符合我的要求。 However, when I click a cluster of markers to spread them, the code above still returns the marker's original position.但是,当我单击一组标记来展开它们时,上面的代码仍然返回标记的原始位置。 See the working example here: https://jsfiddle.net/ebbishop/s2mzgp0b/请参阅此处的工作示例: https : //jsfiddle.net/ebbishop/s2mzgp0b/

I suspect there is a way to get the marker's new position using the oms library, but I haven't been able to find it in the docs.我怀疑有一种方法可以使用 oms 库获取标记的新位置,但我无法在文档中找到它。

How can I retrieve the marker's new position or the change in x & y coordinates?如何检索标记的新位置或 x 和 y 坐标的变化?

The problem was the function getPixelPosition .问题是函数getPixelPosition We can easily change this line我们可以很容易地改变这一行

var loc = marker.data.location , which used a custom, static data attribute set for other purposes, and replace it with var loc = marker.data.location ,它使用自定义的静态数据属性集用于其他目的,并将其替换为

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

All oms does, after a heap of calculations, is update the position of the google maps marker using gmaps' own setPosition method, so this works just fine.经过大量计算后,所有oms都是使用 gmaps 自己的setPosition方法更新谷歌地图标记的位置,所以这很好用。

Updated, working example更新的工作示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM