【问题标题】:Adding Multiple Markers To A React Map向 React Map 添加多个标记
【发布时间】:2020-10-06 11:10:21
【问题描述】:

我正在尝试向我的 Google 地图添加多个标记,但我不确定如何开始,因为我是 React 新手。我想创建一个标记数组并将它们全部渲染到地图上。我该怎么做?

到目前为止的代码:

import React, { Component } from 'react';
import { Map, GoogleApiWrapper, InfoWindow, Marker } from 'google-maps-react';

const mapStyles = {
  width: '100%',
  height: '100%'
};

export class MapContainer extends Component {
  state = {
    showingInfoWindow: false,  
    activeMarker: {},          
    selectedPlace: {}          
  };

  onMarkerClick = (props, marker, e) =>
  this.setState({
    selectedPlace: props,
    activeMarker: marker,
    showingInfoWindow: true
  });

  onClose = props => {
    if (this.state.showingInfoWindow) {
      this.setState({
        showingInfoWindow: false,
        activeMarker: null
      });
    }
  };

  render() {
    return (
      <Map
        google={this.props.google}
        zoom={14}
        style={mapStyles}
        initialCenter={{ lat: 49.166590, lng: -123.133569 }}
      >
        <Marker
          onClick={this.onMarkerClick}
          name={''}
        />

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

我看到了

<Marker
   onClick={this.onMarkerClick}
   name={''}
/>

正在渲染标记,但我如何将它变成一个循环来显示标记数组?

【问题讨论】:

    标签: javascript reactjs google-maps


    【解决方案1】:

    欢迎来到 React 的世界。要渲染一个组件数组,您可以使用array.prototype.map() 映射到您的数组。比如

    const myArray = ['hello', 'world']
      return (
        <div>
          {myArray.map((element, index) => 
            <span key = {index}>{element}</span>}
        </div>
       )
    // equivalent to
    // <div>
    //   <span key = {0}>hello</span>}
    //   <span key = {1}>world</span>}
    // </div>
    

    注意,为每个元素的根节点提供唯一的key 属性很重要。有关渲染数组的更多信息,请参阅 this questionthis tutorial。另见marker documentation

    所以对于你的情况,举个例子,我刚刚在渲染函数中添加了一个markers 数组。

    render() {
    let markers = [ // Just an example this should probably be in your state or props. 
      {
        name: "marker1",
        position: { lat: 49.17655, lng: -123.138564 }
      },
      {
        name: "marker2",
        position: { lat: 49.16659, lng: -123.113569 }
      },
      {
        name: "marker3",
        position: { lat: 49.15659, lng: -123.143569 }
      }
    ];
    return (
      <Map
        google={this.props.google}
        zoom={14}
        style={mapStyles}
        initialCenter={{ lat: 49.16659, lng: -123.133569 }}
      >
        {markers.map((marker, index) => (
          <Marker
            key={index} // Need to be unique
            onClick={this.onMarkerClick}
            name={marker.name}
            position={marker.position}
          />
        ))}
        <InfoWindow
          marker={this.state.activeMarker}
          visible={this.state.showingInfoWindow}
          onClose={this.onClose}
        >
          <div>
            <h4>{this.state.selectedPlace.name}</h4>
          </div>
        </InfoWindow>
      </Map>
    );
    }
    

    这是一个带有状态标记的代码沙盒示例。 (只需在maps.js:4 中添加您的地图 API 密钥即可加载地图)

    【讨论】:

    • 很好的解释,非常感谢!我很难通过 Google 查找所需的信息,但我真的很高兴您能够直截了当!
    猜你喜欢
    • 2017-05-15
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 2017-10-11
    • 1970-01-01
    • 2013-03-10
    相关资源
    最近更新 更多