[Google Maps api with React

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Google Maps api with React相关的知识,希望对你有一定的参考价值。

我目前正在研究一个项目,并多次尝试将可点击的标记添加到我的地图中。我已经使用了google-maps-react库,但是我没有设法获得一个显示信息并能够单击此信息窗口上的按钮的标记。我已经读过默认情况下Marker是不可点击的,但是还有其他方法吗?或是否有任何解释此类的教程或代码预先谢谢你...

答案

您需要在标记上使用.addListener()并告诉它打开一个信息窗口(或您要在标记单击时执行的任何操作。)>

您可以在Google API文档here中找到一个示例

  // Add a marker to the map
  const marker = new google.maps.Marker({
    // position of the marker on map
    position: { lat: 53.3, lng: -6.3 }, 
    // the map object you want the marker on
    map: map, 
  });

  // add a listener to the marker which listens for a 'click' 
  // and runs the callback when it 'hears' one
  marker.addListener('click', () => {
    // set the content of the info window. This can be formatted using html
    infowindow.setContent('I have been clicked!');
    // tells the infowindow on which map to open and which marker to anchor to
    infowindow.open(map, marker);
  });

以上是关于[Google Maps api with React的主要内容,如果未能解决你的问题,请参考以下文章

Google Maps API DirectionsService.route 与 Google Maps Directions 不同

在 maps.google.com 上的缩放比在 Google Maps API v3 上更流畅

Google Maps API V3 中的 API 密钥是啥?

Apple Maps 和 Google Maps API 的参数相同吗?

Google Places API 中缺少 Google Maps 中的地点

新的 Google API 密钥“Google Maps API v3”不适用于 Google Places API?