【问题标题】:React leaflet not rendering properly反应传单没有正确呈现
【发布时间】:2021-02-04 00:45:56
【问题描述】:

react-leaflet 地图未正确渲染。

  • 地图在其父边界之外呈现
  • 地图的某些图块丢失

将地图与标准react 组件一起使用时会出现问题。

我的网站也使用react-bootstrap。正如我所读到的,这可能会对 react-leaflet 的渲染方式造成一些潜在问题。

import React from 'react';
import ReactDOM from 'react-dom';
import { Map, Marker, Popup, TileLayer } from 'react-leaflet';

const position = [37.335556, -122.009167];

class MapView extends React.Component {
    render() {
        return (
                  <div
                    style={{
                        height:"100%"
                    }}>
                    <Map center={position} zoom={13}>
                        <TileLayer
                          url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'
                          attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
                        />
                        <Marker position={position}>
                          <Popup>
                            <span>A pretty CSS3 popup.<br/>Easily customizable.</span>
                          </Popup>
                        </Marker>
                      </Map>
                  </div>
        );
    }
}

module.exports = MapView;

【问题讨论】:

    标签: twitter-bootstrap reactjs leaflet react-bootstrap react-leaflet


    【解决方案1】:

    还将这些 Leaflet 的 CSS 和 JS 文件添加到您的项目中

    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css" />
    <script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet.js"></script>
    

    还可以为地图添加高度。这是强制性的。

    简单JsFiddle 附言寻找外部资源。

    【讨论】:

      【解决方案2】:

      首先,通过控制台安装:

      > npm install leaflet 
      > npm install react-leaflet
      

      在 index.js 中导入一个位于 node_modules 中的 css

      //src/index.js
          import 'leaflet/dist/leaflet.css'
      

      所以,现在只需设置边距和高度:

          ////src/App.js 
      return (
                  <MapContainer center={position} zoom={3} scrollWheelZoom={false} 
              style={{ height:"`enter code here`400px",backgroundColor:"red",marginTop:"80px", marginBottom:'90px'
                  }} >
              </MapContainer>
              )
      

      或

         return (
              <>
                <Map style={{ width: "100%", height: "100vh" }} center={center} zoom={13}>
                  </Map>
      </> )
      

      【讨论】:

        【解决方案3】:

        主要问题是没有导入 CSS,也没有设置 Map 组件的高度。

        然后我使用react-leaflet-marker-layer 修复了缺少标记图像的问题

        import React from 'react';
        import ReactDOM from 'react-dom';
        import { Map, Marker, Popup, TileLayer } from 'react-leaflet';
        import MarkerLayer from 'react-leaflet-marker-layer';
        
        const position = { lng: -122.673447, lat: 45.522558 };
        const markers = [
          {
            position: { lng: -122.67344700000, lat: 45.522558100000 },
            text: 'Voodoo Doughnut',
          },
          {
            position: { lng: -122.67814460000, lat: 45.5225512000000 },
            text: 'Bailey\'s Taproom',
          },
          {
            position: { lng: -122.67535700000002, lat: 45.5192743000000 },
            text: 'Barista'
          },
          {
            position: { lng: -122.65596570000001, lat: 45.5199148000001 },
            text: 'Base Camp Brewing'
          }
        ];
        
        class ExampleMarkerComponent extends React.Component {
        
          render() {
            const style = {
              border: 'solid 1px lightblue',
              backgroundColor: '#333333',
              borderRadius: '50%',
              marginTop: '-5px',
              marginLeft: '-5px',
              width: '10px',
              height: '10px'
            };
        
            return (
              <div style={Object.assign({}, this.props.style, style)}></div>
            );
          }
        
        }
        
        class MapView extends React.Component {
            render() {
                return (
                          <div
                            style={{
                                height:"700px"
                            }}>
                            <Map center={position} zoom={13}
                                style={{
                                    height:"700px"
                                }}>
                                <TileLayer
                                  url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'
                                  attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
                                />
                                <MarkerLayer
                                    markers={markers}
                                    longitudeExtractor={m => m.position.lng}
                                    latitudeExtractor={m => m.position.lat}
                                    markerComponent={ExampleMarkerComponent} />
                              </Map>
                          </div>
                );
            }
        }
        
        module.exports = MapView;
        

        【讨论】:

          猜你喜欢
          • 2019-03-13
          • 2020-03-27
          • 2021-04-23
          • 1970-01-01
          • 2021-12-19
          • 2023-02-09
          • 1970-01-01
          • 2021-11-09
          • 1970-01-01
          相关资源
          最近更新 更多