【问题标题】:Multiple Popups always open多个弹出窗口始终打开
【发布时间】:2016-09-08 11:38:57
【问题描述】:

我想在地图加载时打开地图上的多个弹出窗口,我有这个答案中的示例适用于一个弹出窗口:

Popup always open in the marker

但是当我有多个弹出窗口时,只有一个在加载时打开,打开一个关闭另一个 - 在传单文档 (http://leafletjs.com/reference-1.0.0.html#popup) 中,它建议使用 addLayer 来避免这种情况,但我不知道如何重新创建它在反应传单中:

const React = window.React;
const { Map, TileLayer, Marker, Popup, MapLayer, LayerGroup } = window.ReactLeaflet;

class SimpleExample extends React.Component {
  constructor() {
    super();
    this.state = {
      lat: 51.505,
      lng: -0.09,
      zoom: 13,
    };
  }

  render() {
    const position = [this.state.lat, this.state.lng];
    const position2 = [this.state.lat - 0.01, this.state.lng];
    return (
      <Map center={position} zoom={this.state.zoom}>
        <TileLayer
          attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
          url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'
        />
        <ExtendedMarker position={position}>
          <Popup>
            <span>A pretty CSS3 popup. <br/> Easily customizable.</span>
          </Popup>
        </ExtendedMarker>

        <ExtendedMarker position={position2}>
         <Popup position={position2}>
            <span>A pretty CSS3 popup. <br/> Easily customizable.</span>
          </Popup>
        </ExtendedMarker>


      </Map>
    );
  }
}

// Create your own class, extending from the Marker class.
class ExtendedMarker extends Marker {
    // "Hijack" the component lifecycle.
  componentDidMount() {
    // Call the Marker class componentDidMount (to make sure everything behaves as normal)
    super.componentDidMount();

    // Access the marker element and open the popup.
    this.leafletElement.openPopup();
  }
}

window.ReactDOM.render(<SimpleExample />, document.getElementById('container'));

https://jsfiddle.net/37uo2cp5/

【问题讨论】:

    标签: react-leaflet


    【解决方案1】:

    任何人在这个问题上绊倒,在更高版本的传单(例如:1.0.0+)中,Popup 的选项中有一个选项autoClose,您可以将其设置为false 以显示多个弹出窗口.

    <Map center={position} zoom={this.state.zoom}>
        <TileLayer
          attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
          url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'
        />
        <ExtendedMarker position={position}>
          <Popup autoClose={false}>
            <span>A pretty CSS3 popup. <br/> Easily customizable.</span>
          </Popup>
        </ExtendedMarker>
    
        <ExtendedMarker position={position2}>
         <Popup position={position2} autoClose={false}>
            <span>A pretty CSS3 popup. <br/> Easily customizable.</span>
          </Popup>
        </ExtendedMarker>
    
    
      </Map>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-08
      • 2016-05-11
      • 1970-01-01
      • 2011-04-16
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多