【问题标题】:InfoWindow "View on Google Maps" linkInfoWindow“在 Google 地图上查看”链接
【发布时间】:2018-10-24 00:12:05
【问题描述】:

在以前的谷歌地图版本(3.32-3.33)中,可以在创建Marker 时在MarkerOptions 中指定一个名为place 的属性。然后,当打开 InfoWindow 并使用 Marker 作为锚定位它时,会自动将“在 Google 地图上查看”链接添加到 InfoWindow 内容中。

sample using 3.33 demonstrates this - 单击标记并注意出现在InfoWindow 中的“在 Google 地图上查看”链接。

这种行为曾经是documented at 3.exp which is no longer available(但请参阅https://web.archive.org/web/20171014093717/developers.google.com/maps/documentation/javascript/3.exp/reference),虽然它没有出现,但它已经/曾经记录在official docs for e.g. 3.33 中,但上面引用的示例显示了它的实际作用。

然而,从 3.34 开始,没有“在 Google 地图上查看”链接自动添加到 InfoWindow,请参阅 this sample using the latest version - 它只是引用最新版本的 google 地图的相同示例。

在 3.34 中是否故意删除了此功能?
或者,这是一个错误?
或者,是否应该使用另一种方式通过指定其他选项在 3.34 中将“在 Google 地图上查看”自动添加到 InfoWindow
或者,现在是否必须手动添加这样的链接?

【问题讨论】:

  • 我以前从未见过。但正如名字所说,API 的“实验”版本是,嗯……实验。因此,您可能会在其中看到永远不会发布到发布版本的内容,或者可能会在以后发布的内容。在发布版本中实现之前,@geocodezip 下面提供的答案可以正常工作!检查release notes 以了解未来的版本更新。
  • place 的属性 MarkerOptions 也在非实验文档中,例如请参阅API Reference - Release 3.31,我没有看到任何版本的发行说明中提到它。
  • 抱歉,我没听懂。然后也许它错误地发布了。如果您想了解更多,可以随时在issue tracker 中打开一个新的错误。

标签: google-maps google-maps-api-3 google-maps-markers


【解决方案1】:

我不知道您的问题是否是错误或是否被故意丢弃,但该链接仅指向地点 getDetails 请求返回的地点对象中的 URL。

您可以像这样自己将其添加到信息窗口中:

  infowindow = new google.maps.InfoWindow({
    content: '<div><strong>' + place.name + '</strong><br>' + 'Place ID: ' + place.place_id + '<br>' + 
      place.formatted_address + '</div>'+
      // add "view on google maps
      '<div style="border-top: 1px solid rgb(204, 204, 204); margin-top: 9px; padding: 6px; font-size: 13px; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; font-family: Roboto, Arial;">'+
      '<a href="'+place.url+'" target="_blank" rel="noopener" style="cursor: pointer; color: rgb(66, 127, 237); text-decoration: none;">View on Google Maps</a></div>'
  });

proof of concept fiddle

代码 sn-p:

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    center: {
      lat: -33.866,
      lng: 151.196
    },
    zoom: 15
  });

  var service = new google.maps.places.PlacesService(map);

  service.getDetails({
    placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4'
  }, function(place, status) {
    if (status === google.maps.places.PlacesServiceStatus.OK) {
      var marker = new google.maps.Marker({
          map: map,
          position: place.geometry.location,
          place: {
            location: place.geometry.location,
            placeId: place.place_id
          }
        }),
        infowindow = new google.maps.InfoWindow({
          content: '<div><strong>' + place.name + '</strong><br>' + 'Place ID: ' + place.place_id + '<br>' + place.formatted_address + '</div>' + '<div style="border-top: 1px solid rgb(204, 204, 204); margin-top: 9px; padding: 6px; font-size: 13px; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; font-family: Roboto, Arial;"><a href="' + place.url + '" target="_blank" rel="noopener" style="cursor: pointer; color: rgb(66, 127, 237); text-decoration: none;">View on Google Maps</a></div>' +
            '<div>API version ' + google.maps.version + '</div>'
        });

      google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map, this);
      });
      google.maps.event.trigger(marker, 'click');
    }
  });
}
/* Always set the map height explicitly to define the size of the div
 * element that contains the map. */

#map {
  height: 100%;
}


/* Optional: Makes the sample page fill the window. */

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?libraries=places&callback=initMap">
</script>

【讨论】:

  • 谢谢,但问题是这是否是故意更改功能和/或 3.34+ 中的功能现在是否可以通过其他“手段”/选项/属性获得,而不是如何添加手动这样的链接 - 我知道而且做起来很简单。
猜你喜欢
  • 2012-10-17
  • 2016-04-10
  • 2014-01-02
  • 1970-01-01
  • 1970-01-01
  • 2014-01-28
  • 1970-01-01
  • 1970-01-01
  • 2016-10-31
相关资源
最近更新 更多