Bootstrap React瓣叶未正确渲染

clj7thdc  于 2022-12-07  发布在  Bootstrap
关注(0)|答案(4)|浏览(156)

react-leaflet贴图未正确渲染。

  • Map在其父级边界之外渲染
  • Map的某些切片丢失

将Map与标准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;

9ceoxa92

9ceoxa921#

首先,通过控制台安装:

> 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>
</> )
fiei3ece

fiei3ece2#

也将这些传单的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>

也添加高度到Map。这是强制性的。
简单的JsFiddle P.S.查看外部资源。

6kkfgxo0

6kkfgxo03#

主要问题是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;
rslzwgfq

rslzwgfq4#

也许你可以试试这个,它用于React.js或Next.js,现在它正在react.js 18上工作。
首先,我建议卸载任何传单库,然后安装这一个

npm i leaflet leaflet-defaulticon-compatibility leaflet-geosearch

您可以使用以下代码:

import React, { useState, useEffect, useRef, useMemo, useCallback } from 'react'

const markerRef = useRef(null)
const eventHandlers = useMemo(
        () => ({
            dragend() {
                const marker = markerRef.current
                if (marker != null) {
                    // setPosition(marker.getLatLng())
                    console.log(marker.getLatLng())
                }
            },
        }),
        [],
    )
    useEffect(() => {
     // you will get the current position of the marker here
        console.log(markerRef.current)
    }, [markerRef.current])

//////////////////////
/// in the html

<MapContainer center={[40.8054, -74.0241]} zoom={14} scrollWheelZoom={false} style={{ height: "400px", width: "100%" }}>
    <TileLayer
    attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
        <Marker
            eventHandlers={eventHandlers}
            position={[40.8054, -74.0241]}
            draggable={true}
            animate={true}
            ref={markerRef}
        >
       <Popup>
           Hey ! you found me
       </Popup>
   </Marker>
</MapContainer>

here复制

相关问题