简体   繁体   English

在没有访问令牌的情况下使用 MapBox GL JS

[英]Use MapBox GL JS without Access Token

Is there a way to use MapBox GL JS without access token?有没有办法在没有访问令牌的情况下使用MapBox GL JS I cannot find any hint in the documentation of MapBox GL JS , however, Uber suggest that it is possible with their library , providing React Components for MapBox GL JS .我在MapBox GL JS的文档中找不到任何提示,但是, Uber建议可以使用他们的库,为MapBox GL JS提供React组件

From the documentation of react-map-gl来自react-map-gl的文档

Display Maps Without A Mapbox Token在没有 Mapbox 令牌的情况下显示地图

It is possible to use the map component without the Mapbox service, if you use another tile source (for example, if you host your own map tiles).如果您使用其他图块源(例如,如果您托管自己的地图图块),则可以在没有 Mapbox 服务的情况下使用地图组件。 You will need a custom Mapbox GL style that points to your own vector tile source, and pass it to ReactMapGL using the mapStyle prop.您将需要一个指向您自己的矢量切片源的自定义 Mapbox GL 样式,并使用 mapStyle 道具将其传递给 ReactMapGL。 This custom style must match the schema of your tile source.此自定义样式必须与您的磁贴源的架构相匹配。

Source https://uber.github.io/react-map-gl/#/Documentation/getting-started/about-mapbox-tokens来源https://uber.github.io/react-map-gl/#/Documentation/getting-started/about-mapbox-tokens

Is it possible to use the "native" MapBox GL JS without Access Token?是否可以在没有访问令牌的情况下使用“本机” MapBox GL JS If so, how?如果是这样,如何?

Yep, as the comments mention, just don't set the accessToken and refrain from using any mapbox styles or tiles:是的,正如评论中提到的,只是不要设置 accessToken 并避免使用任何地图框样式或图块:

var map = new mapboxgl.Map({
    container: 'map'
    center: [-74.50, 40],
    zoom: 9
});

Then you can add your layer programmatically via map.addLayer/addSource or just create your own style.json file referencing your tile server and layers.然后你可以通过map.addLayer/addSource编程方式添加你的图层,或者只是创建你自己的 style.json 文件来引用你的切片服务器和图层。 The style specification is documented extensively here: https://docs.mapbox.com/mapbox-gl-js/style-spec/此处详细记录了样式规范: https : //docs.mapbox.com/mapbox-gl-js/style-spec/

As people have already commented you need to add your own data source, stamens have some open tiles services or normal OSM would do.正如人们已经评论过的那样,您需要添加自己的数据源,雄蕊有一些开放的瓷砖服务或正常的 OSM 会做。 Change the style key to be an object with a source and layers parameters.将样式键更改为具有源和图层参数的对象。 The Mapbox style docs are pretty good. Mapbox 风格的文档非常好。 https://docs.mapbox.com/mapbox-gl-js/style-spec/ https://docs.mapbox.com/mapbox-gl-js/style-spec/

I have created a medium post which goes step by step - https://medium.com/@markallengis/simple-web-map-using-mapbox-gl-js-a44e583e0589我创建了一个循序渐进的中等帖子 - https://medium.com/@markallengis/simple-web-map-using-mapbox-gl-js-a44e583e0589

Quick example of what I mean below, note if your service is vector then update type .我在下面的意思的快速示例,请注意您的服务是否为 vector ,然后更新type

style:{
        'version': 8,
        'sources': {
          'raster-tiles': {
            'type': 'raster',
            'tiles': [
              'https://yourtileservicehere/{z}/{x}/{y}.jpg'
            ],
            'tileSize': 256,
          }
        },
        'layers': [{
          'id': 'simple-tiles',
          'type': 'raster',
          'source': 'raster-tiles',
          'minzoom': 0,
          'maxzoom': 22
        }]
      }

Check out this code snipped at: https://docs.mapbox.com/mapbox-gl-js/example/map-tiles/ You can delete the line with "mapboxgl.accessToken" and your good to go.查看在以下位置剪下的代码: https ://docs.mapbox.com/mapbox-gl-js/example/map-tiles/ 您可以删除带有“mapboxgl.accessToken”的行,然后就可以了。

I have just tested it with the ReactMapboxGL component and it works!我刚刚使用 ReactMapboxGL 组件对其进行了测试,并且可以正常工作! Just pass the "mapStyle" prop to the component with the style object from the docs.只需将“mapStyle”道具传递给带有来自文档的样式对象的组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM