【发布时间】:2021-11-27 23:31:50
【问题描述】:
我有下一张传单地图:
但是没有正确显示任何东西,只有当我拖动地图并且只加载您可以在左上角看到的部分时,作为第二张图片。
我发现错误只发生在第一次渲染时,如果我导航到其他页面而不刷新(下一个链接组件),那么它会正确显示。
我的代码:
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-reaflet 和leaflet,我还导入了 CSS 并指定了容器的宽度和高度,正如 Stack Overflow 中的其他解决方案所示,但没有任何效果。
如何在 Next 中导入组件(因为 Leaflet 不支持服务器端渲染):
/**
* Leaflet makes direct calls to the DOM when it is loaded, therefore React Leaflet is not compatible with server-side rendering.
* @see https://stackoverflow.com/a/64634759/9244579
*/
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
});
【问题讨论】:
-
请用
width: 500px试试 -
@FalkeDesign 似乎不起作用:(
-
zoom={100}?对吗? -
可能会在leaflet loads incomplete map找到一些线索
-
是的,我尝试调整缩放但没有@SethLutske
标签: javascript reactjs next.js leaflet react-leaflet