使用react-leaflet时缺少地图瓦片

5
我正在一个简单的create-react-app应用程序中使用leaflet-react简单示例
问题:地图瓦片确实会渲染,但总是有1-2行地图瓦片不渲染(变灰)。当地图移动时,不同的行将开始消失。

enter image description here

然而,如果我调整浏览器窗口大小,无论是多少,地图都会正确显示!

enter image description here

什么是问题,我们如何解决它? 使用react-leaflet v2.2.1,leaflet 1.4.0。在Chrome浏览器和Brave浏览器上存在相同的问题。

Maps.js

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

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

    render() {
        const position = [this.state.lat, this.state.lng];

        return (
            <div>
                <Map center={position} zoom={this.state.zoom}>
                    <TileLayer
                        attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
                        url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'
                    />
                    <Marker position={position}>
                        <Popup>
                            <span>A pretty CSS3 popup. <br/> Easily customizable.</span>
                        </Popup>
                    </Marker>
                </Map>

            </div>

        )
    }
}

export default Maps;

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';

import './index.css';
import Maps from './containers/Maps/Maps.js';

ReactDOM.render(
    <Router>
        <div>
            <Switch>
                <Route path="/" exact component={Maps} />
            </Switch>
        </div>
    </Router>
    , document.getElementById('root'));

index.css

.leaflet-container {
  height: 800px;
  width: 100%;
}

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
    />
    <meta name="theme-color" content="#000000" />
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <title>React App</title>
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.4.0/dist/leaflet.css"
  integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA=="
  crossorigin=""/>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
  </body>
</html>

你能否查看浏览器控制台,看看试图获取这些瓦片的请求发生了什么? - RemcoGerlich
@RemcoGerlich 浏览器JS控制台中没有任何内容。当浏览器窗口大小调整并且灰色区域立即填充瓷砖时,会收到许多.png瓷砖图像的GET请求。 - Nyxynyx
1
尝试 <Map style={{ height: "100vh" }} - kboul
@kboul 这个有效!为什么需要这个? - Nyxynyx
如果我发布答案,您是否介意接受它? - kboul
1
@kboul 当然,请说。 - Nyxynyx
2个回答

7
您只需将height属性传递给您的Map组件实例即可。我相信在此之后,您不需要这段代码:
.leaflet-container {
  height: 800px;
  width: 100%;
}

因此,它应该是。
<Map 
    style={{ height: "100vh" }} 
    center={position} 
    zoom={this.state.zoom}>
      <TileLayer
          attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
          url='http://{s}.tile.osm.org/{z}/{x}/{y}.png' />
      <Marker position={position}>
            <Popup>
                 <span>A pretty CSS3 popup. <br/> Easily customizable.</span>
            </Popup>
      </Marker>
</Map>

演示


6
谢谢。让我的一天变得更美好了。为什么那些使用 react-leaflet 的人不能把一个简单的样例搞定呢?我不明白 :/ - decades
2
如果任何人仍然遇到问题,请确保在您的index.html中有<link rel="stylesheet" href="https://unpkg.com/leaflet@1.4.0/dist/leaflet.css" integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA==" crossorigin=""/> - Giacomo A.
1
为什么这个库的作者没有把这个功能放到他们自己的设置指南中的示例代码中呢? - rm.rf.etc
从今天开始,我使用的是<MapContainer />而不是<Map />,但这个错误似乎仍然存在?至少对于我来说,MapContainer中的style属性不再起作用了...有人遇到同样的问题吗? - ch1ll
你的代码中可能有其他部分出错了。请确保已导入leaflet.css文件。 - kboul
1
我将MapContainer的高度设置为90%,但从未在其中任何div上设置绝对高度。一旦我以绝对像素设置高度,它就起作用了。所以现在还好,但是在项目后期,我需要处理一些CSS:D谢谢你的提示! - ch1ll

4

正如@Giacomo A.评论中提到的

make sure to have:

<link rel="stylesheet" href="https://unpkg.com/leaflet@1.4.0/dist/leaflet.css"  integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA=="  crossorigin=""/> 

in your index.html.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接