React-Leaflet 入门

标签 react-leaflet

它应该是这样的吗?

我关注了https://github.com/PaulLeCam/react-leaflet/blob/master/example/components/simple.js以及主页快速入门。

Sucky map

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "leaflet": "^1.3.4",
    "react": "^16.6.3",
    "react-dom": "^16.6.3",
    "react-leaflet": "^2.1.2",
    "react-scripts": "2.1.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ]
}

最佳答案

查看“react-leaflet”的文档,它指出以下内容:

You will notably need to add its CSS to your page to render the map properly, and set the height of the container (full text: https://react-leaflet.js.org/docs/en/setup.html#docsNav)

您可以尝试设置 Map 元素的宽度和高度(例如,我需要宽度为父容器的 100%,固定高度为 450px):

<Map center={position} zoom={this.state.zoom} style={{width: '100%', height: 450}}>
...

这将为您提供一个带有缩放选项的框,但您不会看到 map 本身,因为您现在缺少 map 样式,因此需要导入它:

@import '~leaflet/dist/leaflet.css';

您还可以在 HTML 中添加 CSS:

<link rel="stylesheet" href="https://unpkg.com/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="68040d090e040d1c2859465d4659" rel="noreferrer noopener nofollow">[email protected]</a>/dist/leaflet.css"
   integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
   crossorigin=""/>

关于React-Leaflet 入门,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53751656/

相关文章:

javascript - 动态边界更改不会在 'react-leaflet' 中产生任何影响

javascript - React-leaflet-search 组件未渲染

reactjs - react 传单 : setting a polygon's style dynamically

javascript - React-Leaflet:如何监听LayersControl.Overlay复选框点击事件?

react-leaflet - react 传单 : Show popup on mouseover

javascript - 向 React Leaflet map 添加标题

next.js - 尝试使用 React-Leaflet(T3 堆栈)构建 nextjs 时出错

javascript - 如何在 react 传单上添加比例?

reactjs - 如何使用 react 传单获得界限

javascript - 在 <div> 标签中包裹 react-leaflet Map 组件使其消失