reactjs - 如何使用 React 组件显示 Google Maps InfoWindow

标签 reactjs google-maps infowindow

我的代码如下,我没有在我的 react 应用程序中使用任何外部库

import React, { Component } from "react";
import MarkerInfo from "./MarkerInfo";
import { render } from "react-dom";

class Map extends Component {
componentDidMount() {
    this.renderMap();
}

renderMarkerInfo = () => {
    return (
    <div id="markerinfo">
        <h1>THis is info marker</h1>
    </div>
    );
};

renderMap = () => {
    loadScript(
    "https://maps.googleapis.com/maps/api/js?key=KEY&callback=initMap"
    );
    window.initMap = this.initMap;
};

initMap = () => {
    var uluru = { lat: -25.363, lng: 131.044 };
    var map = new window.google.maps.Map(document.getElementById("map"), {
    zoom: 4,
    center: uluru
    });

    var contentString =
    '<div id="content">' +
    '<div id="siteNotice">' +
    "</div>" +
    '<h1 id="firstHeading" class="firstHeading">Uluru</h1>' +
    '<div id="bodyContent">' +
    "<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large " +
    "sandstone rock formation in the southern part of the " +
    "Northern Territory, central Australia. It lies 335&#160;km (208&#160;mi) " +
    "south west of the nearest large town, Alice Springs; 450&#160;km " +
    "(280&#160;mi) by road. Kata Tjuta and Uluru are the two major " +
    "features of the Uluru - Kata Tjuta National Park. Uluru is " +
    "sacred to the Pitjantjatjara and Yankunytjatjara, the " +
    "Aboriginal people of the area. It has many springs, waterholes, " +
    "rock caves and ancient paintings. Uluru is listed as a World " +
    "Heritage Site.</p>" +
    '<p>Attribution: Uluru, <a href="https://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">' +
    "https://en.wikipedia.org/w/index.php?title=Uluru</a> " +
    "(last visited June 22, 2009).</p>" +
    "</div>" +
    "</div>";

    var infowindow = new window.google.maps.InfoWindow({
    content: contentString
    });

    var marker = new window.google.maps.Marker({
    position: uluru,
    map: map,
    title: "Uluru (Ayers Rock)"
    });
    marker.addListener("click", function() {
    infowindow.open(map, marker);
    });
};

render() {
    return (
    <div id="map">
        <MarkerInfo />
    </div>
    );
}
}

function loadScript(url) {
    var index = window.document.getElementsByTagName("script")[0];
    var script = window.document.createElement("script");
    script.src = url;
    script.async = true;
    script.defer = true;
    index.parentElement.insertBefore(script, index);
}

export default Map;

当前我正在显示带有硬编码文本的 info 窗口,我想使用 react 组件显示它,我怎样才能做到这一点?

我写了另一个组件,每当我想要动态显示 react 组件的标记时

提前致谢

最佳答案

要考虑的一个选项是:

1) 在此阶段创建 InfoWindow 的实例而不设置 content 属性:

const infowindow = new window.google.maps.InfoWindow({
      content: ""
});

2) 并通过InfoWindow.setContent function 动态地设置更新 内容单击标记后:

marker.addListener("click", () => {
    const content = ReactDOMServer.renderToString(InfoWindowContent);
    infowindow.setContent(content);
    infowindow.open(map, marker);
});

其中 InfoWindowContent 表示为 React 元素和 ReactDOMServer.renderToString function用于将其呈现为 HTML 字符串,因为 InfoWindow.setContent 接受内容值作为字符串:

const InfoWindowContent = (
  <div id="bodyContent">
    <p>
      <b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large sandstone
      rock formation in the southern part of the Northern Territory, central
      Australia. It lies 335&#160;km (208&#160;mi) south west of the nearest
      large town, Alice Springs; 450&#160;km (280&#160;mi) by road. Kata Tjuta
      and Uluru are the two major features of the Uluru - Kata Tjuta National
      Park. Uluru is sacred to the Pitjantjatjara and Yankunytjatjara, the
      Aboriginal people of the area. It has many springs, waterholes, rock caves
      and ancient paintings. Uluru is listed as a World Heritage Site.
    </p>
    <p>
      Attribution: Uluru,
      <a href="https://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">
        https://en.wikipedia.org/w/index.php?title=Uluru
      </a>
      (last visited June 22, 2009).
    </p>
  </div>
);

这里是 demo

关于reactjs - 如何使用 React 组件显示 Google Maps InfoWindow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55411539/

相关文章:

reactjs - ReactCSSTransitionGroup 只适用于新组件中的动画?

javascript - 如何基于 JSON 数组绘制多个多边形

javascript - GMap API 3点击触发链接标记

php - 谷歌地图信息窗口通过XML的多个地址

html - 无法从信息窗口中删除白色边框

javascript - React dropzone 图像未显示在模板上

javascript - if 和 else 条件均被执行

reactjs - 为什么 eslint-plugin-react-hooks 在有条件地使用 React hooks 时不会发出警告?

javascript - 从 Google Places API 调用中提取更多信息

google-maps - Google Maps V3 的多段折线和信息窗口