使用OSM瓦片的react-map-gl无需API密钥

6

有可能吗?

这个链接告诉我可以,但是我不知道为什么它要定义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在2.0版中更改了许可证,因此接受的答案适用于版本< 2.0。如果没有提供access_token,Mapbox > 2.0将会出现错误提示。

1个回答

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 })}
      />

请注意,这只是一个抽象的例子。在这里从OSM加载瓦片有点慢,不能用于生产环境。但它应该说明如何制作地图,而不依赖于Mapbox的服务端。

太棒了。看看我能否将一个独立的示例添加到一些JS代码空间中。 - Hiwa

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