reactjs - 带有反应和 mapbox gl 的 Mapbox GL 应用程序不起作用
问题描述
我按照使用 react 和 Mapbox GL 的 Mapbox 教程进行操作,并且没有与代码相关的问题,但是当我在浏览器(chrome)上启动应用程序时,它会显示一个带有坐标的空白页面。似乎应用程序本身可以工作,但它没有显示地图。
这是我的代码:
html
<!DOCTYPE html>
<html lang='en'>
<head>
<title>Mapbox GL JS and React</title>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.3.1/mapbox-gl.css' rel='stylesheet' />
<link href='site.css' rel='stylesheet'/>
</head>
<body>
<div id='app'></div>
</body>
</html>
CSS:
.sidebarStyle {
display: inline-block;
position: absolute;
top: 0;
left: 0;
margin: 12px;
background-color: #404040;
color: #ffffff;
z-index: 1 !important;
padding: 6px;
font-weight: bold;
}
.mapContainer {
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
}
js:
import React from 'react';
import ReactDOM from 'react-dom';
import mapboxgl from 'mapbox-gl';
mapboxgl.accessToken = 'MAPBOX_ACCESS_TOKEN';
class Application extends React.Component {
constructor(props) {
super(props);
this.state = {
lng: 5,
lat: 34,
zoom: 2
};
}
componentDidMount() {
const map = new mapboxgl.Map({
container: this.mapContainer,
style: 'mapbox://styles/mapbox/streets-v11',
center: [this.state.lng, this.state.lat],
zoom: this.state.zoom
});
map.on('move', () => {
this.setState({
lng: map.getCenter().lng.toFixed(4),
lat: map.getCenter().lat.toFixed(4),
zoom: map.getZoom().toFixed(2)
});
});
}
render() {
return (
<div>
<div className='sidebarStyle'>
<div>Longitude: {this.state.lng} | Latitude: {this.state.lat} | Zoom: {this.state.zoom}</div>
</div>
<div ref={el => this.mapContainer = el} className='mapContainer' />
</div>
)
}
}
ReactDOM.render(<Application />, document.getElementById('app'));
解决方案
推荐阅读
- jsf - 延迟加载由 primefaces tabview 标签管理的选项卡中的输出面板
- regex - 如何防止 \g 子表达式覆盖 Ruby Regex 中的分组内容
- javascript - 为什么 webpack 5 忽略一个文件中的 NODE_ENV,但读取另一个文件
- c# - 通过变量作为字符串的布尔类型的 NPOI C# SetCellValue()
- elixir - 会话到期后,凤凰服务器主索引页面无法正确重定向
- r - 组合多个矩阵并应用百分比和括号格式
- sql-server - 如何将列值与声明的变量进行比较
- python - 如何解决 IPython 中 Selenium 的“进程意外关闭,状态为 11”?
- java - 更新页面和单选按钮在 html 中消失
- xmonad - 在 xmonad 中,有些键绑定有效,有些则无效?