MAPBOX GL加载2个地图
因此,Mapbox提供的示例代码似乎已损坏。我已经将他们的示例映射组件复制到了地图。JS,由于某种原因,我获得了2个地图而不是1个地图。
import * as React from "react";
import mapboxgl from 'mapbox-gl';
mapboxgl.accessToken = 'API KEY';
export default class Mapy extends React.Component {
constructor(props) {
super(props);
this.state = {
lng: -70.9,
lat: 42.35,
zoom: 9
};
this.mapContainer = React.createRef();
}
componentDidMount() {
const { lng, lat, zoom } = this.state;
const map = new mapboxgl.Map({
container: this.mapContainer.current,
style: 'mapbox://styles/mapbox/streets-v11',
center: [lng, lat],
zoom: zoom
});
map.on('move', () => {
this.setState({
lng: map.getCenter().lng.toFixed(4),
lat: map.getCenter().lat.toFixed(4),
zoom: map.getZoom().toFixed(2)
});
});
}
render() {
const { lng, lat, zoom } = this.state;
return (
<div>
<div className="sidebar">
Longitude: {lng} | Latitude: {lat} | Zoom: {zoom}
</div>
<div ref={this.mapContainer} className="map-container" />
</div>
);
}
}
So the sample code provided by Mapbox seems to be broken. I have copied their sample map component into a map.js and for some reason, I am getting 2 maps instead of 1.
import * as React from "react";
import mapboxgl from 'mapbox-gl';
mapboxgl.accessToken = 'API KEY';
export default class Mapy extends React.Component {
constructor(props) {
super(props);
this.state = {
lng: -70.9,
lat: 42.35,
zoom: 9
};
this.mapContainer = React.createRef();
}
componentDidMount() {
const { lng, lat, zoom } = this.state;
const map = new mapboxgl.Map({
container: this.mapContainer.current,
style: 'mapbox://styles/mapbox/streets-v11',
center: [lng, lat],
zoom: zoom
});
map.on('move', () => {
this.setState({
lng: map.getCenter().lng.toFixed(4),
lat: map.getCenter().lat.toFixed(4),
zoom: map.getZoom().toFixed(2)
});
});
}
render() {
const { lng, lat, zoom } = this.state;
return (
<div>
<div className="sidebar">
Longitude: {lng} | Latitude: {lat} | Zoom: {zoom}
</div>
<div ref={this.mapContainer} className="map-container" />
</div>
);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有同样的问题&amp;通过添加以下内容来修复。
I had the same issue & fixed by adding the below.