尝试在 Google 地图 API 中实施 "shouldFocus" infoWindowOpen 选项

Trying to implement "shouldFocus" infoWindowOpen Option in Google Maps API

当在 Google Maps infoWindow 中使用 infoWindow.open 方法时,有人可以提供一个在 Javascript 中使用的“shouldFocus”布尔选项的简单示例吗?

InfoWindowOpenOptions interface

...

infoWindow.open(地图,标记); // 打开 infoWindow 但不要将焦点转移到它 - 如何?

...

调用 open 时,如果要指定选项对象,则无需提供地图。它将在 MVCObject(标记等)所在的地图上呈现 window。

infoWindow.open({
    anchor: marker,
    shouldFocus:false
});

这将阻止 InfoWindow 获得焦点。

编辑 2

我已经用3.43版本(8月中旬删除)重新测试了,还是可以的。所以恕我直言,这是版本 3.44 中引入的错误,当时还引入了 属性 shouldFocus

当前存在描述该问题的未解决问题: https://issuetracker.google.com/issues/190075886 https://issuetracker.google.com/issues/190637181

编辑

我刚刚创建了一个 fiddle,其中给定的方法在某些情况下 起作用。我无法重现为什么会这样。我创建了一个 fiddle,它在 2.5 秒和 5 秒后打开给定的 InfoWindow

案例一: 2.5 秒后,InfoWindow 打开,但没有聚焦。如果InfoWindow打开后立即关闭,5秒后没有被聚焦

Link 到案例 1 fiddle: https://jsfiddle.net/ywnj6bc4/5/

案例二: 如果 2.5 秒后 InfoWindow 打开但没有关闭,则 InfoWindow 在 5 秒后被聚焦。

Link 到案例 2 fiddle: https://jsfiddle.net/ywnj6bc4/4/

我仍在为我的案例寻找合适的解决方法,因为我还没有发现究竟是什么导致了这个——在我看来——错误的结果。

原回答

我刚刚遇到了同样的问题。主要是因为我使用的 InfoWindow 没有与之关联的标记。

我的解决方法是创建 InfoWindow,然后通过添加“假”标记作为锚点选项打开它。

如文档中所述,可能还有其他方法可以实现此目的:

If anchor is non-null, position this InfoWindow at the top-center of the anchor, instead of at the given latlng. The InfoWindow will be rendered on the same Map or StreetView as the anchor.

查看此代码示例。

var map = new google.maps.Map({some: options})

let infoWindow = new google.maps.InfoWindow({
  content: 'Info Window Content...',
  position: {lat: 47.5, lng: 8.5},
});

infoWindow.open({
  shouldFocus: false, 
  anchor: new google.maps.Marker({
    anchor: infoWindow.position, // same position as infoWindow
    map: map
  })
});