【问题标题】:I'm trying to number my Markers and show InfoWindow with google-maps-react我正在尝试为我的标记编号并使用 google-maps-react 显示 InfoWindow
【发布时间】:2019-09-29 01:38:53
【问题描述】:

我似乎无法显示从我的 json 到 google-maps-react 的索引,我可以看到所有已映射的标记,但它们显示了默认标记,没有弹出窗口。这是放置<InfoWindow> 的代码,反应抱怨我必须放置一个父div,当我这样做时,我目前看不到任何标记。

我的 car2go json 映射正确,只是没有打印出 name={content.name}

我的 map.js 组件:

import React, { Component } from "react";
import Car2go from "../data/car2go/vehicles.json";
import { Map, InfoWindow, Marker, GoogleApiWrapper } from "google-maps-react";

export class MapContainer extends Component {
  constructor(props) {
    super(props);
    this.onMarkerClick = this.onMarkerClick.bind(this);
    this.state = {
      showingInfoWindow: false,
      activeMarker: {},
      selectedPlace: {},
      name: null
    };
  }
  onMarkerClick(props, marker, e) {
    this.setState({
      selectedPlace: props,
      activeMarker: marker,
      showingInfoWindow: true
    });
  }
  render() {
    const google = window.google;
    const style = {
      width: "70%",
      height: "70%",
      margin: "0 auto"
    };
    //const google = window.google;
    return (
      <Map
        google={this.props.google}
        style={style}
        initialCenter={{
          lat: 53.59301,
          lng: 10.07526
        }}
        zoom={12}
        onClick={this.onMapClicked}
      >
        {Car2go.placemarks.map((content, index) => {
          return (
            <div>
              <Marker
                title={index}
                name={content.name}
                position={{
                  lat: content.coordinates[1],
                  lng: content.coordinates[0]
                }}
                onClick={this.onMarkerClick}
                name={content.name}
              />

              <InfoWindow
                marker={this.state.activeMarker}
                visible={this.state.showingInfoWindow}
              >
                <div>
                  <h1>{this.state.selectedPlace.name}</h1>
                </div>
              </InfoWindow>
            </div>
          );
        })}
      </Map>
    );
  }
}

export default GoogleApiWrapper({
  apiKey: "xxxxx"
})(MapContainer);

【问题讨论】:

    标签: reactjs google-maps google-maps-react


    【解决方案1】:

    我猜 React 正在抱怨类似于这个的错误:

    React 无法识别 DOM 元素上的 mapCenter 属性

    如果是这样,则此错误的根本原因与使用 div 容器包装 Marker 组件有关:

    <div>
        <Marker 
           ...  
        />
    </div>
    

    以及google-maps-react Map 组件渲染子元素的方式。在这种情况下,Map 道具被转移到div 元素而不是Marker 组件。更多详情请参阅Unknown Prop Warning 文章。

    为了规避这个错误,可以考虑以下方法:

    • div 容器替换为React.Fragment
    • 将地图道具显式转移到Marker组件

    这是一个例子:

    class Map extends Component {
      constructor() {
        super();
        this.state = {
          map: null
        };
        this.handleMapReady = this.handleMapReady.bind(this);
      }
    
      handleMapReady(mapProps, map) {
        this.setState({ map: map });
      }
    
      render() {
        return (
          <Map
            google={this.props.google}
            className={"map"}
            initialCenter={this.props.center}
            zoom={this.props.zoom}
            onReady={this.handleMapReady}
          >
            {this.state.map &&
              places.map((place, i) => {
                const mapProps = Object.assign({}, this.props);
                mapProps.map = this.state.map;
                return (
                  <React.Fragment key={i}>
                    <Marker
                      {...mapProps}
                      onClick={this.handleMarkerClick}
                      position={place.position}
                      placeIndex={i}
                      name={place.name}
                    />
                  </React.Fragment>
                );
              })}
          </Map>
        );
      }
    }
    

    但我会提出另一种方法,而不是上述更改,特别是创建InfoWindow 组件的单个实例 并按如下所示进行管理:

    <Map
        google={this.props.google}
        className={"map"}
        initialCenter={this.props.center}
        zoom={this.props.zoom}
      >
        {places.map((place, i) => {
          return (
            <Marker
              key={i}
              onClick={this.handleMarkerClick}
              position={place.position}
              placeIndex={i}
              name={place.name}
            />
          );
        })}
        <InfoWindow
          marker={this.state.activeMarker}
          visible={this.state.showingInfoWindow}
          onClose={this.handleClose}
        >
          <div>{this.state.selectedPlace.name}</div>
        </InfoWindow>
      </Map>
    

    Here is a demo

    【讨论】:

      猜你喜欢
      • 2020-02-17
      • 2021-05-28
      • 2021-02-19
      • 2019-01-18
      • 1970-01-01
      • 2019-01-31
      • 2018-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多