【发布时间】:2016-07-30 15:55:26
【问题描述】:
我正在使用LeafletJS 插件在ReactJS 单页应用程序中显示整页地图。我遵循了here 的指示。但是,图块的显示顺序不正确。它们是随机排序的(?)。我发现了一个类似的问题here,但在这种情况下,订单在页面刷新时是固定的。这对我来说不是真的。我的代码是(不包括样板代码):
index.html
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="stylesheets/atlas.css">
</head>
<body>
<div id="app">
Loading...
</div>
<script src="javascripts/atlas.js"></script>
</body>
</html>
index.js
render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('app')
)
App.jsx
import LiveMap from './LiveMap';
// App component
const App = () => (
<LiveMap />
)
export default App;
LiveMap.jsx
class LiveMap extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div id='map'></div>
);
}
componentDidMount() {
let _map = this.map = L.map(ReactDOM.findDOMNode(this)).setView([-41.2858, 174.78682], 14);
L.tileLayer(
'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://openstreetmap.org">OpenStreetMap</a> Contributors',
maxZoom: 18,
}).addTo(_map);
}
componentWillUnmount() {...}
shouldComponentUpdate() {return false;}
};
export default LiveMap;
【问题讨论】:
-
在渲染地图之前尝试设置超时。
componentDidMount可能会有些误导,因为元素已插入 DOM 但未完全呈现(没有宽度和高度)。 Leaflet 需要宽度和高度才能正确渲染... -
您的
atlas.css和atlas.js文件是由什么组成的?打乱的磁贴通常是未加载leaflet.css样式表的症状。 -
@SkipJack 试过了。没有帮助。
-
@ghybs 我尝试显式加载
leaflet.css。还是没有成功。 -
@341008 如果不是这两件事,那我有点不知所措..你能添加一个这个问题的活生生的例子吗?
标签: javascript reactjs ecmascript-6 leaflet