【问题标题】:Bug display with react leaflet带有反应传单的错误显示
【发布时间】:2019-01-02 22:04:47
【问题描述】:

我从 react 开始,我想使用一个名为“react-leaflet”的模块来制作地图。 但问题是我的地图最后在我的页面上有一个显示错误。 你能帮帮我吗?

组件图:

import './Map.css';
import React, { Component } from 'react';
import { Map, Marker, Popup, TileLayer } from 'react-leaflet';

export default class MapLeaflet extends Component {

    constructor(props) {
        super(props);
        this.state = {
            lat: 51.505,
            lng: -0.09,
            zoom: 13
        }
    }

    render() {
        const position = [this.state.lat, this.state.lng];
        console.log("dqdsqdq");
        return(
            <div id="map">
                <Map center={position} zoom={13}>
                    <TileLayer
                        url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
                        attribution="&copy; <a href=&quot;http://osm.org/copyright&quot;>OpenStreetMap</a> contributors"
                    />
                    <Marker position={position}>
                        <Popup>A pretty CSS3 popup.<br />Easily customizable.</Popup>
                    </Marker>
                </Map>
            </div>
        );
    }
}

显示地图的页面:

import './Apies.css';
import React, { Component } from 'react';
import MapLeaflet from '../../components/Map/Map.js';

export default class Apies extends Component {

    render() {
        return (
            <section className="Apies">
                <main className="main">
                    <h2>Map</h2>
                    <MapLeaflet/>
                </main>
            </section>
        );
    }
}

Bug 展示:https://imgur.com/ojNDL3Z

【问题讨论】:

  • 你有什么办法解决这个问题?控制台有错误吗?
  • 不,我的控制台没有任何错误
  • 您是否为地图设置了高度或是否导入了leaflet.css?试试&lt;Map style={{height: '100vh'}} center={position} zoom={13}&gt;
  • 不,我不知道如何导入传单 css。我试过你的命令,但没有效果。

标签: reactjs leaflet react-leaflet


【解决方案1】:

需要在你的index.js中导入leaflet.css,并给地图容器一个高度。

index.js:

import "leaflet/dist/leaflet.css";

地图传单:

  ...
<Map style={{ height: "100vh" }} center={position} zoom={13}>

Demo

【讨论】:

  • 谢谢,我明天试试,结果告诉你
  • 当然。没问题
  • 谢谢你,它有效,但我没有看到我的标记。我的控制台中有两个错误。错误“data:image/png;base64, marker-icon.png:1”和“data:image/png;base64, marker-shadow.png:1”
  • 我更新了演示链接。立即检查,如果对您有帮助,请接受答案
猜你喜欢
  • 2018-01-02
  • 2021-12-19
  • 2021-02-28
  • 2017-09-22
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 2017-09-14
相关资源
最近更新 更多