如何使用React组件显示Google Maps InfoWindow [英] How to use a react component for showing Google Maps InfoWindow

查看:132
本文介绍了如何使用React组件显示Google Maps InfoWindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下,我未在我的React应用程序中使用任何外部库

i have my code as follows, i am not using any external libraries inside my react app

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 window,我想使用react组件来显示它,我怎么能做到这一点?

current I am showing the info window with the hardcoded text, I want to show it using a react component, how can I be able to do that?

我写了另一个组件,只要我想动态显示标记组件

i wrote another component, whenever the marker i want to show the react component dynamically

预先感谢

推荐答案

要考虑的一个选项是:

1)在不设置content属性的情况下创建InfoWindow的实例:

1) to create an instance of InfoWindow without setting content property at this stage:

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

2)和 set update 内容通过

2) and set or update content dynamically via InfoWindow.setContent function once a marker is clicked:

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

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

where InfoWindowContent is represented as a React element and ReactDOMServer.renderToString function is used to render it into HTML string since InfoWindow.setContent accepts content value as a string:

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>
);

这是演示

这篇关于如何使用React组件显示Google Maps InfoWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆