繁体   English   中英

向 React Map 添加多个标记

[英]Adding Multiple Markers To A React Map

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

到目前为止的代码:

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={''}
/>

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

欢迎来到 React 的世界。 要渲染组件数组,您可以使用array.prototype.map()通过您的数组 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 prop 很重要。 有关渲染 arrays 的更多信息,请参阅此问题本教程 另请参阅标记文档

因此,对于您的情况,举个例子,我刚刚向渲染 function 添加了一个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>
);
}

这是一个代码沙盒示例,其中包含 state 中的标记。 (只需在maps.js:4中添加您的地图 API 键即可加载地图)

编辑 stoic-visvesvaraya-yc2qs

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM