【问题标题】:react-map-gl without API key using osm tilesreact-map-gl 没有使用 osm 瓦片的 API 密钥
【发布时间】:2018-12-07 12:39:06
【问题描述】:

有可能吗?

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

但我无法让它与react-map-gl StaticMap 类一起使用。我可以从该类中看到的属性只是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>

谢谢

编辑:从正确答案开始,为了保持 SO,这是生活在 S3 上的json

{
  "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"
}

更新: Mapbox changed 他们在 2.0 中的许可证,因此接受的答案对于 2.0 会报错。

【问题讨论】:

    标签: mapbox openstreetmap react-map-gl


    【解决方案1】:

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

    这是基本组件的外观,与Github repo 中的示例不同:

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

    请注意,这只是一个抽象示例。这里从 OSM 加载的瓦片有点太慢了,无法在生产中使用。但它应该说明如何在不依赖 Mapbox 服务端的情况下制作地图。

    【讨论】:

    • 太棒了。看看我是否可以在一些 JS 代码空间中添加一个独立的示例。
    猜你喜欢
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 2016-04-28
    • 2017-04-19
    • 1970-01-01
    相关资源
    最近更新 更多