[英]Map not showing in react leaflet
為什么地圖沒有顯示在反應傳單中?
import { MapContainer, Marker, TileLayer, Popup } from "react-leaflet";
import "leaflet/dist/leaflet.css";
<div style={{ width: "100%", height: "100vh" }}>
<MapContainer center={[51.505, -0.09]} zoom={13} scrollWheelZoom={false}>
<TileLayer
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.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 App;
您錯過了向MapContainer
添加樣式表,現在它的高度為 0。 您應該將樣式style={{ width: "100%", height: "100vh" }}
添加到MapContainer
,看看下面的代碼:
import { MapContainer, Marker, TileLayer, Popup } from "react-leaflet";
import "leaflet/dist/leaflet.css";
export default function App() {
return (
<div>
<MapContainer
style={{ width: "100%", height: "100vh" }}
zoom={13}
center={[51.505, -0.09]}
scrollWheelZoom={false}
fadeAnimation={true}
markerZoomAnimation={true}
>
<TileLayer
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<Marker position={[51.505, -0.09]}>
<Popup>
A pretty CSS3 popup. <br /> Easily customizable.
</Popup>
</Marker>
</MapContainer>
</div>
);
}
現場演示: https ://codesandbox.io/s/throbbing-cherry-s5mb4p?file=/src/App.js:0-735
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.