使用 React 缺少 MapBox CSS
Missing MapBox CSS using React
我正在尝试使用 React 来处理 MapBox。我已经使用 create-react-app 创建了项目,添加了 mapbox-gl ("mapbox-gl": "^0.46.0-beta.1"),但是 css 文件有问题。它向我显示了这个警告:
This page appears to be missing CSS declarations for Mapbox GL JS, which may cause the map to display incorrectly. Please ensure your page includes mapbox-gl.css, as described in
https://www.mapbox.com/mapbox-gl-js/api/
我已完成所有步骤:
1 - 安装 npm 包:npm install --save mapbox-gl
2 - 在您的 HTML 文件中包含 CSS 文件:<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.45.0/mapbox-gl.css' rel='stylesheet' />
。
但是我如何使用与 ES6 的反应,我在 index.js css 文件 import 'mapbox-gl/dist/mapbox-gl.css';
中添加了
如果我导入 css 文件,它不会显示任何内容,如果我评论导入它会显示没有设计的地图,它会显示警告。
我该如何解决???感谢您的帮助
这是我的代码:
import React, { Component } from 'react';
import mapboxgl from 'mapbox-gl';
mapboxgl.accessToken = 'my_token';
class MapComponent extends Component {
constructor(props) {
super(props);
this.state = {
lng: 1.2217,
lat: 39.8499,
zoom: 6.48
};
}
componentDidMount() {
const { lng, lat, zoom } = this.state;
var map = new mapboxgl.Map({
container: 'map',
center: [lng, lat],
style: 'mapbox://styles/mapbox/streets-v10',
zoom: zoom
});
map.on('move', () => {
const { lng, lat } = map.getCenter();
this.setState({
lng: lng.toFixed(4),
lat: lat.toFixed(4),
zoom: map.getZoom().toFixed(2)
});
});
}
render() {
const { lng, lat, zoom } = this.state;
return (
<div className="App">
<div className="inline-block absolute top left mt12 ml12 bg-darken75 color-white z1 py6 px12 round-full txt-s txt-bold">
<div>{`Longitude: ${lng} Latitude: ${lat} Zoom: ${zoom}`}</div>
</div>
<div id="map" />
</div>
);
}
}
export default MapComponent;
解决方案:
正如我所说,我添加了 mapbox-gl.css,import 'mapbox-gl/dist/mapbox-gl.css';
但没有显示地图。
在 css 文件上显示下一个 css 属性:
<canvas class="mapboxgl-canvas" tabindex="0" aria-label="Map" width="951" height="844" style="position: absolute; width: 951px; height: 844px;"></canvas>
我已经修改了 canvas 个属性:
.mapboxgl-canvas {
position: fixed !important;
height: 100% !important;
}
已解决:
1 - import 'mapbox-gl/dist/mapbox-gl.css';
2 - 覆盖 css class 称为 .mapboxgl-canvas
.mapboxgl-canvas {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
height: 100%;
}
1 - 导入 'mapbox-gl/dist/mapbox-gl.css';
2 - 覆盖 css class 调用的 .mapboxgl-canvas
.mapboxgl-canvas {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 100%;
height: 100%;
}
1 - 创建一个 randomName.css 文件
2 - 将此代码添加到您的 randomName.css 文件
#yourMapDivName {
position:absolute;
top:0;
bottom:0;
width:100%;
}
body {
margin:0;
padding:0;
}
3 - 将“./randomName.css”导入您的 index.js 文件
使用旧版本的 Mapbox 样式表时也会显示警告as described here:
Adding the stylesheet like this doesn't fix the warning:
<!-- index.html -->
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.42.0/mapbox-gl.css' rel='stylesheet' />
And importing in the app.js like this doesn't work either:
// app.js
import 'mapbox-gl/dist/mapbox-gl.css';
The way I found that fix this problem with the current version of the module is add the updated stylesheet:
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.53.0/mapbox-gl.css' rel='stylesheet' />
我遇到了同样的问题,这解决了它。
我正在尝试使用 React 来处理 MapBox。我已经使用 create-react-app 创建了项目,添加了 mapbox-gl ("mapbox-gl": "^0.46.0-beta.1"),但是 css 文件有问题。它向我显示了这个警告:
This page appears to be missing CSS declarations for Mapbox GL JS, which may cause the map to display incorrectly. Please ensure your page includes mapbox-gl.css, as described in
https://www.mapbox.com/mapbox-gl-js/api/
我已完成所有步骤:
1 - 安装 npm 包:npm install --save mapbox-gl
2 - 在您的 HTML 文件中包含 CSS 文件:<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.45.0/mapbox-gl.css' rel='stylesheet' />
。
但是我如何使用与 ES6 的反应,我在 index.js css 文件 import 'mapbox-gl/dist/mapbox-gl.css';
如果我导入 css 文件,它不会显示任何内容,如果我评论导入它会显示没有设计的地图,它会显示警告。
我该如何解决???感谢您的帮助
这是我的代码:
import React, { Component } from 'react';
import mapboxgl from 'mapbox-gl';
mapboxgl.accessToken = 'my_token';
class MapComponent extends Component {
constructor(props) {
super(props);
this.state = {
lng: 1.2217,
lat: 39.8499,
zoom: 6.48
};
}
componentDidMount() {
const { lng, lat, zoom } = this.state;
var map = new mapboxgl.Map({
container: 'map',
center: [lng, lat],
style: 'mapbox://styles/mapbox/streets-v10',
zoom: zoom
});
map.on('move', () => {
const { lng, lat } = map.getCenter();
this.setState({
lng: lng.toFixed(4),
lat: lat.toFixed(4),
zoom: map.getZoom().toFixed(2)
});
});
}
render() {
const { lng, lat, zoom } = this.state;
return (
<div className="App">
<div className="inline-block absolute top left mt12 ml12 bg-darken75 color-white z1 py6 px12 round-full txt-s txt-bold">
<div>{`Longitude: ${lng} Latitude: ${lat} Zoom: ${zoom}`}</div>
</div>
<div id="map" />
</div>
);
}
}
export default MapComponent;
解决方案:
正如我所说,我添加了 mapbox-gl.css,import 'mapbox-gl/dist/mapbox-gl.css';
但没有显示地图。
在 css 文件上显示下一个 css 属性:
<canvas class="mapboxgl-canvas" tabindex="0" aria-label="Map" width="951" height="844" style="position: absolute; width: 951px; height: 844px;"></canvas>
我已经修改了 canvas 个属性:
.mapboxgl-canvas {
position: fixed !important;
height: 100% !important;
}
已解决:
1 - import 'mapbox-gl/dist/mapbox-gl.css';
2 - 覆盖 css class 称为 .mapboxgl-canvas
.mapboxgl-canvas {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
height: 100%;
}
1 - 导入 'mapbox-gl/dist/mapbox-gl.css';
2 - 覆盖 css class 调用的 .mapboxgl-canvas
.mapboxgl-canvas {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 100%;
height: 100%;
}
1 - 创建一个 randomName.css 文件
2 - 将此代码添加到您的 randomName.css 文件
#yourMapDivName {
position:absolute;
top:0;
bottom:0;
width:100%;
}
body {
margin:0;
padding:0;
}
3 - 将“./randomName.css”导入您的 index.js 文件
使用旧版本的 Mapbox 样式表时也会显示警告as described here:
Adding the stylesheet like this doesn't fix the warning:
<!-- index.html --> <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.42.0/mapbox-gl.css' rel='stylesheet' />
And importing in the app.js like this doesn't work either:
// app.js import 'mapbox-gl/dist/mapbox-gl.css';
The way I found that fix this problem with the current version of the module is add the updated stylesheet:
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.53.0/mapbox-gl.css' rel='stylesheet' />
我遇到了同样的问题,这解决了它。