简体   繁体   English

当标记在Google Maps V3的外部视图中时,InfoWindow无法通过链接正确打开

[英]InfoWindow doesn't open correctly with link when marker is outside view on Google Maps V3

I made a map with markers clustered on Google Maps. 我用标记聚集在Google Maps上的地图制作了地图。 The makers have a listener that set the zoom, then center the map and then open a infowindow. 制造商有一个监听器,用于设置缩放比例,然后将地图居中,然后打开一个信息窗口。

In a for loop I made a list with links to every marker that looks like this 在for循环中,我制作了一个列表,其中包含指向每个看起来像这样的标记的链接

<a href="javascript:google.maps.event.trigger(markers['+i+'],\'click\');">' + item.nombre + '</a>

If I click a marker on the map there is no problem: the map is centered and the infowindow is showed. 如果我单击地图上的标记,则没有问题:地图居中并显示信息窗口。

The problem is if I press a link form the list: if the maker is not showing on the map the infowindow is open incorrectly (in a wrong position and overlapping an icon) and the map is not centered (the map doesn't even show the correct place of the marker). 问题是,如果我按列表中的链接:如果制造商未在地图上显示,则信息窗口打开不正确(位置错误且图标重叠),并且地图未居中(地图甚至不显示)标记的正确位置)。
像这样
Strangely, if I zoom out the map at a point that the real position of the marker is showed the InfoWindow is set correctly on the correct marker and there is no problem if I press the same link again, just like in the second image. 奇怪的是,如果我在显示标记的实际位置的位置缩小地图,则将InfoWindow正确地设置在正确的标记上,并且再次按下相同的链接也没有问题,就像第二幅图像一样。
没问题

Here is the jsfiddle , the error could be reproduced if I click on Playa Maqui Lodge and then on any other link using Firefox 43 这是jsfiddle ,如果我单击Playa Maqui Lodge ,然后使用Firefox 43单击任何其他链接,则可能会重现该错误。

So, what am I missing? 那么,我想念什么? Why the map is not centering on the link? 为什么地图没有以链接为中心?

The marker isn't added to the map until it is "unclustered", zoom to it then "click" on it. 标记要先“放大”,然后再对其进行“单击”,然后才将其添加到地图。 The zoom must be higher than the maxZoom of the markerClusterer. 缩放比例必须高于markerClusterer的maxZoom。

Either set the markerClusterer maxZoom to 15 or change your code to zoom in closer to the marker. 可以将markerClusterer maxZoom设置为15,或者更改代码以将其放大到更接近标记。

google.maps.event.addListener(marker, 'click', function() {
  infowindow.close();
  map.setZoom(21);
  map.setCenter(this.getPosition());
  infowindow.setContent(this.html);
  infowindow.open(map, this);
});

proof of concept fiddle 概念证明

Looks like there is an issue with the computation of the anchor using a custom infowindow (which I didn't see before as you didn't provide your custom marker). 看起来使用自定义信息窗口计算锚点存在问题(由于您未提供自定义标记,所以我之前没有看到过)。 It works if you don't use the InfoWindow.open(map, anchor) syntax and set the pixelOffset and position of the InfoWindow manually. 如果您不使用InfoWindow.open(map, anchor)语法并手动设置InfoWindow的pixelOffsetposition ,则此方法有效。

var infowindow = new google.maps.InfoWindow({pixelOffset:new google.maps.Size(0,-35)});

google.maps.event.addListener(marker, 'click', function() {
  infowindow.close();
  map.setZoom(16);
  map.setCenter(this.getPosition());
  infowindow.setContent(this.html);
  infowindow.setPosition(this.getPosition());
  infowindow.open(map);
});

updated proof of concept fiddle (with custom marker) 更新了概念证明小提琴(带有自定义标记)

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

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