尝试加载 react-leaflet 地图时出错
Getting error while trying to loading a react-leaflet map
我正在尝试加载传单地图,但出现错误:
Attempted import error: 'Map' is not exported from 'react-leaflet' (imported as 'LeafletMap').
我尝试再次安装 react-leaflet 并尝试在我的 App.js 文件中导入 leaflet/dist/leaflet.css
,但它仍然向我显示错误。这是代码
Map.js
import { Map as LeafletMap, TileLayer } from "react-leaflet";
import "./Map.css";
function Maps({ center, zoom }) {
return (
<div className="map">
<LeafletMap center={center} zoom={zoom}>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
/>
</LeafletMap>
</div>
);
}
export default Maps;
App.js
import './App.css'
import Maps from './Maps'
import 'leaflet/dist/leaflet.css'
function App() {
const [mapCenter, setMapCenter] = useState({ lat: 34.80746, lng: -40.4796})
const [mapZoom, setMapZoom] = useState(3)
return (
<div className="app">
<Maps
center= {mapCenter}
zoom= {mapZoom}
/>
</div>
)
}
export default App;
也许你没有使用正确版本的 react-leaflet(你的不导出 Map)。
请参阅我的工作样本:https://codesandbox.io/s/react-leaflet-forked-mg8x4?file=/src/index.js。我用的是 1.3.4
您的地图没有大小,可能问题出在这里
<Map
style={{ height: "100%", width: "100%" }}
center={position}
zoom={zoom}
>
您错误地导入了您的组件。
如果您使用的是 react-leaflet 2.x.x,则需要像下面的示例一样导入 Map。
如果你使用 react-leaflet 3.0.5,会有一些变化。
我在这个 CodeSandBox.
中用最后一个 react-leaflet 做了一个例子
Map.js
import { Map, TileLayer } from "react-leaflet";
import "leaflet/dist/leaflet.css";
import "./Map.css";
function Maps({ center, zoom }) {
return (
<div className="map">
<Map center={center} zoom={zoom}
{/* Give your map a size */}
style={{ height: "100%", width: "100%" }}
>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
/>
</Map>
</div>
);
}
export default Maps;
App.js
import './App.css'
import Maps from './Maps'
function App() {
const [mapCenter, setMapCenter] = useState({ lat: 34.80746, lng: -40.4796})
const [mapZoom, setMapZoom] = useState(3)
return (
<div className="app">
<Maps
center= {mapCenter}
zoom= {mapZoom}
/>
</div>
)
}
export default App;
我正在尝试加载传单地图,但出现错误:
Attempted import error: 'Map' is not exported from 'react-leaflet' (imported as 'LeafletMap').
我尝试再次安装 react-leaflet 并尝试在我的 App.js 文件中导入 leaflet/dist/leaflet.css
,但它仍然向我显示错误。这是代码
Map.js
import { Map as LeafletMap, TileLayer } from "react-leaflet";
import "./Map.css";
function Maps({ center, zoom }) {
return (
<div className="map">
<LeafletMap center={center} zoom={zoom}>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
/>
</LeafletMap>
</div>
);
}
export default Maps;
App.js
import './App.css'
import Maps from './Maps'
import 'leaflet/dist/leaflet.css'
function App() {
const [mapCenter, setMapCenter] = useState({ lat: 34.80746, lng: -40.4796})
const [mapZoom, setMapZoom] = useState(3)
return (
<div className="app">
<Maps
center= {mapCenter}
zoom= {mapZoom}
/>
</div>
)
}
export default App;
也许你没有使用正确版本的 react-leaflet(你的不导出 Map)。 请参阅我的工作样本:https://codesandbox.io/s/react-leaflet-forked-mg8x4?file=/src/index.js。我用的是 1.3.4
您的地图没有大小,可能问题出在这里
<Map
style={{ height: "100%", width: "100%" }}
center={position}
zoom={zoom}
>
您错误地导入了您的组件。 如果您使用的是 react-leaflet 2.x.x,则需要像下面的示例一样导入 Map。 如果你使用 react-leaflet 3.0.5,会有一些变化。 我在这个 CodeSandBox.
中用最后一个 react-leaflet 做了一个例子Map.js
import { Map, TileLayer } from "react-leaflet";
import "leaflet/dist/leaflet.css";
import "./Map.css";
function Maps({ center, zoom }) {
return (
<div className="map">
<Map center={center} zoom={zoom}
{/* Give your map a size */}
style={{ height: "100%", width: "100%" }}
>
<TileLayer
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
/>
</Map>
</div>
);
}
export default Maps;
App.js
import './App.css'
import Maps from './Maps'
function App() {
const [mapCenter, setMapCenter] = useState({ lat: 34.80746, lng: -40.4796})
const [mapZoom, setMapZoom] = useState(3)
return (
<div className="app">
<Maps
center= {mapCenter}
zoom= {mapZoom}
/>
</div>
)
}
export default App;