简体   繁体   English

React Leaflet - TileLayer 首次加载时不渲染

[英]React Leaflet - TileLayer does not render when it is first loaded

This is my first time using React-Leaflet.这是我第一次使用 React-Leaflet。 I am doing the same as shown in the documentation , but the map is grayed out when the page is first loaded.我正在执行与文档中所示相同的操作,但首次加载页面时 map 显示为灰色。 However if i were to resize the browser, the map renders properly and shows map.但是,如果我要调整浏览器的大小,map 会正确呈现并显示 map。

index.js index.js

<link
    rel="stylesheet"
    href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css"
    integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
    crossorigin=""
/>

package.json package.json

"react-leaflet": "^3.0.2",
"leaflet": "^1.7.1",

css file css文件

.leaflet-container {
    height: 310px;
    width: 440px;
    margin: 0 auto;
}

myComponent我的组件

import React from "react";
import { MapContainer, TileLayer, Marker, Popup } from "react-leaflet";

const Location = () => {
   return (
     <div className="location-container">
       <MapContainer
        style={{ height: "310px" }}
        center={[51.505, -0.09]}
        zoom={15}
        scrollWheelZoom={false}
       >
        <TileLayer
          attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
          url="http://{s}.tile.osm.org/{z}/{x}/{y}.png"
        />
        <Marker position={[51.505, -0.09]}>
          <Popup>
            A pretty CSS3 popup. <br /> Easily customizable.
          </Popup>
        </Marker>
     </MapContainer>
   </div>
  );
};
export default Location;

i added leaflet-css to my index.js, i added width and height to my leaflet DOM.我在 index.js 中添加了 Leaflet-css,在 leaflet DOM 中添加了宽度和高度。 I noticed that when it is first loaded, the pictures are not coming from the api, but when I resize browser, the pictures come.我注意到第一次加载时,图片不是来自 api,但是当我调整浏览器大小时,图片来了。 Here are some pictures.这里有一些图片。

first loaded首次加载

灰屏

图像未加载

after resize the browser调整浏览器大小后

地图正确显示 正确加载的图像

I figured it out myself.我自己想通了。 I write if anyone faced the same problem.如果有人遇到同样的问题,我会写信。

I looked through the documentation of the react-leaflet and found this in the section on react-hooks.我查看了 react-leaflet 的文档,并在 react-hooks 部分找到了这一点。 I added useMap to access map functions in React-leaflet.我添加了useMap来访问 React-leaflet 中的 map 函数。

import { MapContainer, TileLayer, Marker, Popup, useMap } from "react-leaflet";

Then, I created a new component on the same page.然后,我在同一页面上创建了一个新组件。

const ResizeMap = () => {
  const map = useMap();
  map._onResize();
  return null;
};

Here I called the _onResize method.这里我调用了 _onResize 方法。 Thus, when the page is first loaded, it resizes the location of the map and the images are loaded properly.因此,当第一次加载页面时,它会调整 map 的位置,并且正确加载图像。 On the other projects they solved this with map.invalidateSize();在其他项目中,他们使用map.invalidateSize();解决了这个问题。

I used the resize component here.我在这里使用了调整大小组件。 It has to be inside the MapContainer.它必须在 MapContainer 内。

     <MapContainer
        style={{ height: "310px" }}
        center={[51.505, -0.09]}
        zoom={15}
        scrollWheelZoom={false}
      >
        <ResizeMap />
        <TileLayer
          attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
          url="http://{s}.tile.osm.org/{z}/{x}/{y}.png"
        />
        <Marker position={[51.505, -0.09]}>
          <Popup>
            A pretty CSS3 popup. <br /> Easily customizable.
          </Popup>
        </Marker>
      </MapContainer>

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

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