使用 React 缺少 MapBox CSS

Jav*_*ier 6 mapbox reactjs mapbox-gl-js

我正在尝试使用 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 inhttps://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;
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

解决方案:

正如我所说,我添加了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>
Run Code Online (Sandbox Code Playgroud)

我通过以下方式修改了画布属性:

.mapboxgl-canvas {
    position: fixed !important;
    height: 100% !important;
}
Run Code Online (Sandbox Code Playgroud)

解决了:

1 - import 'mapbox-gl/dist/mapbox-gl.css'; 2 - 覆盖名为 .mapboxgl-canvas 的 css 类

.mapboxgl-canvas {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
}
Run Code Online (Sandbox Code Playgroud)

Jav*_*ier 6

1 - 导入'mapbox-gl/dist/mapbox-gl.css';

2 - 覆盖名为 .mapboxgl-canvas 的 css 类

.mapboxgl-canvas {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 100%;
}
Run Code Online (Sandbox Code Playgroud)


小智 1

  1. 将 Mapbox 样式表导入到您的index.scss文件中

    import 'mapbox-gl/dist/mapbox-gl.css';

  2. 将index.scss文件导入到您的app.js

    import './styles/index.scss';

  3. 将地图 div 放入 div 包装器中,并为其指定 css 规则。默认情况下,Mapbox 为地图 div 提供 100% 的宽度和高度:

    <div class="myMapWrapper"><div id="map" class="mapboxgl-map" style="position: relative; width: 100%; height: 100%;"></div></div>`
    
    Run Code Online (Sandbox Code Playgroud)

这样你就可以根据你的需要在CSS中调整它。全屏,例如:

 .myMapWrapper {
    height: 100vh;
    width: 100vw;
}
Run Code Online (Sandbox Code Playgroud)