react-map-gl 没有 API 密钥使用 osm 瓷砖

Hiw*_*iwa 6 openstreetmap mapbox react-map-gl

是否可以?

告诉我它是,但不知道它为什么定义和 API 密钥。

但我无法让它与react-map-glStaticMap 类一起使用。我可以从该类中看到的属性只是mapStyle采用标准的 Mapbox 矢量切片路径/名称。它需要一个对象吗?我的代码没有给我错误或显示我请求的图块。

    <DeckGL>
        <StaticMap
            mapStyle= {{
                "version": 7,
                "sources": {
                  "simple-tiles": {
                    "type": "raster",
                    "tiles":["http://a.tile.openstreetmap.org/{z}/{x}/{y}.png", "http://b.tile.openstreetmap.org/{z}/{x}/{y}.png"],
                    "tileSize": 256
                  },
                  "power": {
                  "type": "vector",
                  "tiles": ["http://gpstrails.info/ex/leaflet/power/osm/{z}/{x}/{y}.json"]
                }
                },
                "layers": [{
                  "id": "simple-tiles",
                  "type": "raster",
                  "source": "simple-tiles",
                  "minzoom": 0,
                  "maxzoom": 22
                },
                {
                "id": "road",
                "source": "power",
                "source-layer": "power",
                "type": "line",
                "layout": {
                  "line-join": "round",
                  "line-cap": "round",
                },
                "paint": {
                  "line-color": "red",
                  "line-width": 4,
                }
              }]
              }}/>
    </DeckGL>
Run Code Online (Sandbox Code Playgroud)

谢谢

编辑:从正确的答案,并保持在 SO,这是jsonS3 上的生活:

{
  "version": 8,
  "name": "OSM",
  "metadata": {

  },
  "sources": {
    "openmaptiles": {
      "type": "vector",
      "url": "https://free.tilehosting.com/data/v3.json?key={key}"
    },
    "osm": {
      "type": "raster",
      "tiles": [
        "http://tile.openstreetmap.org/{z}/{x}/{y}.png"
      ],
      "minzoom": 0,
      "maxzoom": 14
    },
    "91y5159eg": {
      "type": "vector",
      "url": "http://localhost:3000/tilejson.json"
    }
  },
  "sprite": "https://openmaptiles.github.io/klokantech-basic-gl-style/sprite",
  "glyphs": "https://free.tilehosting.com/fonts/{fontstack}/{range}.pbf?key=undefined",
  "layers": [
    {
      "id": "osm",
      "type": "raster",
      "source": "osm"
    }
  ],
  "id": "klokantech-basic"
}

Run Code Online (Sandbox Code Playgroud)

更新: Mapbox在 2.0 中更改了他们的许可证,因此对于 <2.0 版本,接受的答案是正确的。如果没有提供 access_token,Mapbox > 2.0 会报错。

bri*_*oft 9

诀窍在于使用的样式。样式是一个 JSON 对象,您可以在此处阅读有关其规范的更多信息。您可以使用Maputnik等工具生成自定义样式,这是一种可视化编辑器,可生成用于 MapboxGL 地图的样式兼容文件。一旦生成了合适的样式,就可以在 React Map GL 中使用它。

以下是基本组件的外观,与Github 存储库中的示例有所不同:

<ReactMapGL
        mapStyle="https://s3.amazonaws.com/cdn.brianbancroft.io/assets/osmstyle.json"
        {...this.state.viewport}
        onViewportChange={viewport => this.setState({ viewport })}
      />
Run Code Online (Sandbox Code Playgroud)

请注意,这只是一个抽象的例子。此处来自 OSM 的 tile 加载有点太慢而无法用于生产。但它应该说明如何在不依赖 Mapbox 的服务端的情况下制作地图。