我有下一个leaflet地图:
但是它没有正确地显示任何内容,只有当我拖动地图并且只加载您在左上角可以看到的部分时,就像第二张图片。
我发现错误只会在第一次渲染时发生,如果我在不刷新的情况下导航到其他页面(下一个Link组件),那么它将正确显示。
我的代码:
import React, { useState } from "react";
import { MapContainer, Marker, Popup, TileLayer } from "react-leaflet";
import "leaflet/dist/leaflet.css";
function Map({ coordinates }) {
const [position, setPosition] = useState(coordinates);
return (
<MapContainer center={position} zoom={100} scrollWheelZoom={false} style={{ height: "500px", width: "100%" }}>
<TileLayer attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
<Marker position={position}>
<Popup>
A pretty CSS3 popup. <br /> Easily customizable.
</Popup>
</Marker>
</MapContainer>
);
}
export default Map;
我已经安装了
react-leaflet
和leaflet
,也导入了CSS并指定了容器的宽度和高度,正如Stack Overflow中其他解决方案所指示的一样,但仍然无法工作。由于leaflet不支持服务器端渲染,因此我应该如何在Next.js中导入组件?/**
* Leaflet makes direct calls to the DOM when it is loaded, therefore React Leaflet is not compatible with server-side rendering.
* @see https://dev59.com/jOk5XIcBkEYKwwoY49on#64634759
*/
const Map = dynamic(() => import("../../components/Map"), {
loading: () => <p>Map is loading</p>,
ssr: false, // This line is important. It's what prevents server-side render
});
宽度:500像素
。 - Falke Designzoom={100}
?这样对吗? - Seth Lutske