我试着使用function加载传单,但是出现了一个错误,上面写着"Map容器找不到“。我找到了一个解决方案,就是将<div id="map">添加到DOM中。我无法在函数组件中找到这样的方法。最后,我使用了类组件来完成这个任务,它起作用了:
import React from "react";
import L, { LeafletMouseEvent } from "leaflet";
import "leaflet/dist/leaflet.css";
import icon from "leaflet/dist/images/marker-icon.png";
import iconShadow from "leaflet/dist/images/marker-shadow.png";
let DefaultIcon = L.icon({
iconUrl: icon,
shadowUrl: iconShadow,
});
class LeafletMap extends React.Component {
componentDidMount() {
this.map();
}
map() {
L.Marker.prototype.options.icon = DefaultIcon;
var mapSW = new L.Point(0, 960),
mapNE = new L.Point(960, 0);
var map = L.map("map", { crs: L.CRS.Simple }).setView([0, 0], 4);
L.tileLayer("/assets/maps/map0/{z}/{x}/{y}.png", {
tileSize: 32,
minZoom: 4,
maxZoom: 5,
noWrap: true,
}).addTo(map);
var maxBounds = L.latLngBounds(
map.unproject(mapSW, map.getMaxZoom()),
map.unproject(mapNE, map.getMaxZoom())
);
map.setMaxBounds(maxBounds);
var marker = L.marker([0, 0], {
draggable: true,
}).addTo(map);
marker.bindPopup("<b>Our hero!</b>").openPopup();
function onMapClick(e: LeafletMouseEvent) {
let tileSize = new L.Point(32, 32);
let pixelPoint = map.project(e.latlng, map.getMaxZoom()).floor();
let coords = pixelPoint.unscaleBy(tileSize).floor()
alert("You clicked the map at " + coords);
}
map.on("click", onMapClick);
}
render() {
return <div id="map"></div>;
}
}
export default LeafletMap;这就是调用LeafletMap组件的地方:
const comp: React.FC<RouteComponentProps> = () => {
return (
<div>
<Grid columns={2}>
<Grid.Column>
<LeafletMap/>
</Grid.Column>
// codes
</Grid>
</div>现在我需要使用钩子特性,所以我必须使用函数组件。如何解决"Map容器未找到“错误或使用函数组件将map元素添加到DOM?
发布于 2021-01-07 07:35:25
从您所包含的代码来看,您似乎不是在使用react-leaflet,而是使用本地传单代码。
使用类组件作为函数不应该是个问题。用效果钩子和删除呈现方法替换componentDidMount
export default function LeafletMap() {
useEffect(() => {
map();
}, []);
...rest of code same as yours only remove render method
return <div id="map" style={{ height: "100vh" }}></div>;
}由于您正在使用类型记录,可能错误来自那里,因为一个错误的类型。
我使用了一个openstreet map贴片提供程序(因为您使用的是自定义的)和一些固定的图标,它似乎没有错误。
发布于 2021-05-21 22:16:18
我也犯了同样的错误,解决方案就在这里,
同样的问题是传单:找不到地图容器
所引起的错误
在调用id=(‘map’)之前,必须将div L.map“map”添加到dom。
解决办法是使用:
useEffect(() => {这是我的工作app.js:
import React, { useState, useEffect } from 'react';
import './App.css';
import L from 'leaflet';
import 'leaflet/dist/leaflet.css';
function App() {
// Similar to componentDidMount and componentDidUpdate:
useEffect(() => {
let current_lat = 28.625789;
let current_long = 77.0547899;
let current_zoom = 16;
let center_lat = current_lat;
let center_long = current_long;
let center_zoom = current_zoom;
// The <div id="map"> must be added to the dom before calling L.map('map')
let map = L.map('map', {
center: [center_lat, center_long],
zoom: center_zoom
});
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution: '© <a href="https://openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
});
return (
<div class="right-sidebar-container">
<div id="map">
</div>
</div>
);
} // app
export default App;https://stackoverflow.com/questions/65604960
复制相似问题