在重叠标记蜘蛛网上的InfowDindows

发布于 2025-02-02 17:50:39 字数 925 浏览 4 评论 0原文

我使用的是Google Maps API V3,带有标记层和重叠标记蜘蛛网。它运行良好,但是我对每个标记附加的Infowdindows有一个问题。当指定一组标记并用键或鼠标打开InfowDOW,相对于标记的真实位置而不是蜘蛛状位置,InfowDindow的位置是定位的。有什么办法可以让InfowDindow跟随蜘蛛侠?

OMS指定:

var oms = new OverlappingMarkerSpiderfier(map, {
  markersWontMove: true,
  markersWontHide: true,
  keepSpiderfied: true,
  legWeight: 1
    });

google.maps.event.addListener(marker, 'click', function(marker, e) {
  return function() {
    var myHTML = e.feature.getProperty('html');
    boxText.innerHTML = '<div class="ib">' + myHTML + '</div>';
    infowindow.setPosition(e.feature.getGeometry().get());
    infowindow.setOptions({
      pixelOffset: new google.maps.Size(0, -42)
    });
    infowindow.open(map);
  };
}(marker, e));

https://jsfiddle.net/groxmzej/

I am using Google Maps API V3 with Marker Clusterer and Overlapping Marker Spiderfier. It is working quite well but I have an issue with the infowindows attached to each marker. When a group of markers are specified and an infowindow is opened with a click or mouseover, the infowindow is positioned relative to the true position of the marker and not the spiderfied position. Is there any way to get the infowindow to follow the spiderfier?

OMS is specified with:

var oms = new OverlappingMarkerSpiderfier(map, {
  markersWontMove: true,
  markersWontHide: true,
  keepSpiderfied: true,
  legWeight: 1
    });

And the infowindows with:

google.maps.event.addListener(marker, 'click', function(marker, e) {
  return function() {
    var myHTML = e.feature.getProperty('html');
    boxText.innerHTML = '<div class="ib">' + myHTML + '</div>';
    infowindow.setPosition(e.feature.getGeometry().get());
    infowindow.setOptions({
      pixelOffset: new google.maps.Size(0, -42)
    });
    infowindow.open(map);
  };
}(marker, e));

https://jsfiddle.net/groxmzej/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彼岸花ソ最美的依靠 2025-02-09 17:50:39

infowdow

文档

打开([选项,锚])
参数:

  • 选项:InfowoWindowOpenoptions | Map | StreetViewPanorama 可选的要么是InfowoWindowopenoptions对象(推荐)或MAP | Panorama渲染此InfowWindow。
  • :mvcobject 可选将其放置在该infoWindow的锚点。如果锚是非锚,则InfowDindow将位于锚的顶部中心。 Infowdoudow将在与锚的同一地图或全景图上渲染(如果有的话)。
    返回值:无
    在给定的地图上打开此Infowdow。可选地,Infowdow可以与锚点关联。在核心API中,唯一的锚是标记类。但是,锚点可以是任何MVCOBJECT,它可以公开LATLNG位置属性,并且可以选择用于计算PixelOffset的点锚点属性(请参阅InfowWindowOptions)。锚点是从锚位置到Infowdindow尖端的偏移。建议将InfowoWopoPenoptions接口用作此方法的单个参数。为防止更改浏览器的关注点,请将InfowoWopoPenoptions.shouldfocus设置为false。

因此,更改您的代码,该代码从:

infowindow.setPosition(e.feature.getGeometry().get());
infowindow.setOptions({
  pixelOffset: new google.maps.Size(0, -42)
});
infowindow.open(map);

<

infowindow.open(map, marker);

a

a href = “ https://jsfiddle.net/geocodezip/geocodezip/nv0lc9x6/1/” =“ https://i.sstatic.net/5lthn.png” rel =“ nofollow noreferrer”>

代码段:

var geoJson = {
  "type": "FeatureCollection",
  "features": [{
      "type": "Feature",
      "properties": {
        "name": "Arthur George Melhuish",
        "html": "<h2>Arthur George Melhuish</h2>Born 12 Jan 1909 in 2 Fore St, Tiverton<br><span class=addedby>Added by Admin</span>"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [-3.48882160, 50.90258030]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "Harold Edward Melhuish",
        "html": "<h2>Harold Edward Melhuish</h2>Born 30 Jun 1903 in 2 Fore Street, Tiverton<br><span class=addedby>Added by Admin</span>"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [-3.48882160, 50.90258030]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "John Norman Melhuish",
        "html": "<h2>John Norman Melhuish</h2>Born 3 Jan 1905 in 2 Fore Street, Tiverton<br><span class=addedby>Added by Admin</span>"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [-3.48882160, 50.90258030]
      }
    },

  ]
};
var map = null;
var bounds = new google.maps.LatLngBounds();

var boxText = document.createElement("div");

var infowindow = new google.maps.InfoWindow({
  content: boxText,
  disableAutoPan: false,
  zIndex: null,
  alignBottom: true,
  enableEventPropagation: false,
  boxStyle: {
    padding: "6px",
    border: "1px solid #666",
    background: "#fff",
    width: "240px"
  }
});



function initialize() {
  var mapOptions = {
    center: new google.maps.LatLng(51, -3.5),
    zoom: 9
  };
  map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
  google.maps.event.addListenerOnce(map, 'idle', function() {
    var oms = new OverlappingMarkerSpiderfier(map, {
      markersWontMove: true,
      markersWontHide: true,
      keepSpiderfied: true,
      legWeight: 1
    });

    oms.addListener('spiderfy', function(spidered, unspidered) {
      for (var i = 0; i < spidered.length; i++) {
        spidered[i].setLabel("");
        spidered[i].setOptions({
          zIndex: i
        });
      }
    });
    oms.addListener('unspiderfy', function(spidered, unspidered) {
      for (var i = 0; i < spidered.length; i++) {
        spidered[i].setLabel("" + (i + 1));
        spidered[i].setOptions({
          zIndex: i
        });
      }
    });

    google.maps.event.addListener(map.data, 'addfeature', function(e) {
      if (e.feature.getGeometry().getType() === 'Point') {
        var marker = new google.maps.Marker({
          position: e.feature.getGeometry().get(),
          /*title: e.feature.getProperty('name'),*/
          map: map
        });

        google.maps.event.addListener(marker, 'mouseover', function(marker, e) {
          return function() {
            var myHTML = e.feature.getProperty('html');
            boxText.innerHTML = '<div class="ib">' + myHTML + '</div>';
            infowindow.open(map, marker);
          };
        }(marker, e));

        google.maps.event.addListener(marker, 'click', function(marker, e) {
          return function() {
            var myHTML = e.feature.getProperty('html');
            boxText.innerHTML = '<div class="ib">' + myHTML + '</div>';
            infowindow.open(map, marker);
          };
        }(marker, e));


        oms.addMarker(marker);
        google.maps.event.addListener(map, 'idle', function() {
          var markersNear = oms.markersNearMarker(marker, false);
          if (markersNear.length > 0) {
            marker.setLabel("" + (markersNear.length + 1));
          }
          marker.setOptions({
            zIndex: markersNear.length
          });
        });
      }
    });
    layer = map.data.addGeoJson(geoJson);
    map.data.setMap(null);
    google.maps.event.addListener(map, "click", function() {
      infowindow.close();
    });
  });

}

google.maps.event.addDomListener(window, 'load', initialize);
#map_canvas {
  position: relative;
  width: 100%;
  height: calc(95vh)
}

.ib {
  line-height: 18px
}

.ib h2 {
  margin: 0;
  line-height: 28px
}

.addedby {
  float: right;
  color: #999;
  margin-top: 1em
}
<!DOCTYPE HTML>
<html>

<head>
  <meta charset="utf-8" />

  <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>

  <script src="https://jawj.github.io/OverlappingMarkerSpiderfier/bin/oms.min.js"></script>

</head>

<body>

  <div id="map_canvas"></div>

</body>

</html>

Use the marker reference as the "anchor" in the call to open on the InfoWindow

documentation

open([options, anchor])
Parameters:

  • options: InfoWindowOpenOptions|Map|StreetViewPanorama optional Either an InfoWindowOpenOptions object (recommended) or the map|panorama on which to render this InfoWindow.
  • anchor: MVCObject optional The anchor to which this InfoWindow will be positioned. If the anchor is non-null, the InfoWindow will be positioned at the top-center of the anchor. The InfoWindow will be rendered on the same map or panorama as the anchor (when available).
    Return Value: None
    Opens this InfoWindow on the given map. Optionally, an InfoWindow can be associated with an anchor. In the core API, the only anchor is the Marker class. However, an anchor can be any MVCObject that exposes a LatLng position property and optionally a Point anchorPoint property for calculating the pixelOffset (see InfoWindowOptions). The anchorPoint is the offset from the anchor's position to the tip of the InfoWindow. It is recommended to use the InfoWindowOpenOptions interface as the single argument for this method. To prevent changing browser focus on open, set InfoWindowOpenOptions.shouldFocus to false.

So change your code that opens the infowindow from:

infowindow.setPosition(e.feature.getGeometry().get());
infowindow.setOptions({
  pixelOffset: new google.maps.Size(0, -42)
});
infowindow.open(map);

To:

infowindow.open(map, marker);

proof of concept fiddle

screenshot showing infowindow open on spiderified marker

code snippet:

var geoJson = {
  "type": "FeatureCollection",
  "features": [{
      "type": "Feature",
      "properties": {
        "name": "Arthur George Melhuish",
        "html": "<h2>Arthur George Melhuish</h2>Born 12 Jan 1909 in 2 Fore St, Tiverton<br><span class=addedby>Added by Admin</span>"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [-3.48882160, 50.90258030]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "Harold Edward Melhuish",
        "html": "<h2>Harold Edward Melhuish</h2>Born 30 Jun 1903 in 2 Fore Street, Tiverton<br><span class=addedby>Added by Admin</span>"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [-3.48882160, 50.90258030]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "John Norman Melhuish",
        "html": "<h2>John Norman Melhuish</h2>Born 3 Jan 1905 in 2 Fore Street, Tiverton<br><span class=addedby>Added by Admin</span>"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [-3.48882160, 50.90258030]
      }
    },

  ]
};
var map = null;
var bounds = new google.maps.LatLngBounds();

var boxText = document.createElement("div");

var infowindow = new google.maps.InfoWindow({
  content: boxText,
  disableAutoPan: false,
  zIndex: null,
  alignBottom: true,
  enableEventPropagation: false,
  boxStyle: {
    padding: "6px",
    border: "1px solid #666",
    background: "#fff",
    width: "240px"
  }
});



function initialize() {
  var mapOptions = {
    center: new google.maps.LatLng(51, -3.5),
    zoom: 9
  };
  map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
  google.maps.event.addListenerOnce(map, 'idle', function() {
    var oms = new OverlappingMarkerSpiderfier(map, {
      markersWontMove: true,
      markersWontHide: true,
      keepSpiderfied: true,
      legWeight: 1
    });

    oms.addListener('spiderfy', function(spidered, unspidered) {
      for (var i = 0; i < spidered.length; i++) {
        spidered[i].setLabel("");
        spidered[i].setOptions({
          zIndex: i
        });
      }
    });
    oms.addListener('unspiderfy', function(spidered, unspidered) {
      for (var i = 0; i < spidered.length; i++) {
        spidered[i].setLabel("" + (i + 1));
        spidered[i].setOptions({
          zIndex: i
        });
      }
    });

    google.maps.event.addListener(map.data, 'addfeature', function(e) {
      if (e.feature.getGeometry().getType() === 'Point') {
        var marker = new google.maps.Marker({
          position: e.feature.getGeometry().get(),
          /*title: e.feature.getProperty('name'),*/
          map: map
        });

        google.maps.event.addListener(marker, 'mouseover', function(marker, e) {
          return function() {
            var myHTML = e.feature.getProperty('html');
            boxText.innerHTML = '<div class="ib">' + myHTML + '</div>';
            infowindow.open(map, marker);
          };
        }(marker, e));

        google.maps.event.addListener(marker, 'click', function(marker, e) {
          return function() {
            var myHTML = e.feature.getProperty('html');
            boxText.innerHTML = '<div class="ib">' + myHTML + '</div>';
            infowindow.open(map, marker);
          };
        }(marker, e));


        oms.addMarker(marker);
        google.maps.event.addListener(map, 'idle', function() {
          var markersNear = oms.markersNearMarker(marker, false);
          if (markersNear.length > 0) {
            marker.setLabel("" + (markersNear.length + 1));
          }
          marker.setOptions({
            zIndex: markersNear.length
          });
        });
      }
    });
    layer = map.data.addGeoJson(geoJson);
    map.data.setMap(null);
    google.maps.event.addListener(map, "click", function() {
      infowindow.close();
    });
  });

}

google.maps.event.addDomListener(window, 'load', initialize);
#map_canvas {
  position: relative;
  width: 100%;
  height: calc(95vh)
}

.ib {
  line-height: 18px
}

.ib h2 {
  margin: 0;
  line-height: 28px
}

.addedby {
  float: right;
  color: #999;
  margin-top: 1em
}
<!DOCTYPE HTML>
<html>

<head>
  <meta charset="utf-8" />

  <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>

  <script src="https://jawj.github.io/OverlappingMarkerSpiderfier/bin/oms.min.js"></script>

</head>

<body>

  <div id="map_canvas"></div>

</body>

</html>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文