mapbox升级后不再开源

1.简介

1.mapbox收费定价及免费版本

  • 参考链接
    https://docs.mapbox.com/mapbox-gl-js/guides/pricing/
    https://www.geoapify.com/mapbox-gl-new-license-and-6-free-alternatives

  • Mapbox GL JS > v2.0.0 --收费

  • 建议使用Mapbox GL JS = v1.13.3 --免费的最新版

版本差异:

Mapbox GL JS v1.x.x和v2.x.x之间的一个重要变化是在触发地图加载(map load)时机上的不同。在v1.x.x版本中,地图加载发生在初始化Mapbox GL JS地图对象并请求Mapbox托管的地图瓦片时。而在v2.x.x版本中,地图加载发生在初始化Mapbox GL JS地图对象时,无论是否请求Mapbox托管的地图瓦片。

结论:

Mapbox GL JS的旧版本(v1)是免费开源的。该版本在发布时采用了免费且开放的BSD3许可证。

Mapbox GL JS v2,采用了专有的非免费许可证,即新版本(v2)不再免费。

2.测试代码

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Display a map on a webpage</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v2.14.1/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.14.1/mapbox-gl.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script>
    var mapStyle = {
            "version": 8,
            "name": "Custom Map",
            "sources": {
                "custom-raster": {
                    "type": "raster",
                    "tiles": ["http://t5.tianditu.gov.cn/img_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=6c6245339bf350c75a69c14125d14cb5"],
                    "tileSize": 256
                }
            },
            // "sprite": "https://demotiles.maplibre.org/styles/osm-bright-gl-style/sprite",
            "glyphs": "./fonts/glyphs/{fontstack}/{range}.pbf",
            "layers": [
                {
                    "id": "custom-raster-layer",
                    "type": "raster",
                    "source": "custom-raster"
                }
            ]

        };
	// mapboxgl.accessToken = 'pk.eyJ1IjoiYWRtaW4tbHVjYXMiLCJhIjoiY2xrcm5jcDFnMGJvMjNmbjNrdWh1dDQ3biJ9.gW0WN4QSSl1EZPmgrhUc0Q';
    const map = new mapboxgl.Map({
        container: 'map', // container ID
        // Choose from Mapbox's core styles, or make your own style with Mapbox Studio
        style: mapStyle, // style URL
        center: [-74.5, 40], // starting position [lng, lat]
        zoom: 9 // starting zoom
    });
</script>

</body>
</html>

查看控制台

在这里插入图片描述

查看网络资源加载

在这里插入图片描述

3.低版本就能加载

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Display a map on a webpage</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v1.3.1/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v1.3.1/mapbox-gl.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script>
    var mapStyle = {
            "version": 8,
            "name": "Custom Map",
            "sources": {
                "custom-raster": {
                    "type": "raster",
                    "tiles": ["http://t5.tianditu.gov.cn/img_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=6c6245339bf350c75a69c14125d14cb5"],
                    "tileSize": 256
                }
            },
            // "sprite": "https://demotiles.maplibre.org/styles/osm-bright-gl-style/sprite",
            "glyphs": "./fonts/glyphs/{fontstack}/{range}.pbf",
            "layers": [
                {
                    "id": "custom-raster-layer",
                    "type": "raster",
                    "source": "custom-raster"
                }
            ]

        };
	// mapboxgl.accessToken = 'pk.eyJ1IjoiYWRtaW4tbHVjYXMiLCJhIjoiY2xrcm5jcDFnMGJvMjNmbjNrdWh1dDQ3biJ9.gW0WN4QSSl1EZPmgrhUc0Q';
    const map = new mapboxgl.Map({
        container: 'map', // container ID
        // Choose from Mapbox's core styles, or make your own style with Mapbox Studio
        style: mapStyle, // style URL
        center: [-74.5, 40], // starting position [lng, lat]
        zoom: 9 // starting zoom
    });
</script>

</body>
</html>

在这里插入图片描述

4.结论

  • 高版本说明是先加载的地图服务,然后mapbox判断是否用token,我没用所以不能显示。
  • 低版本就可以用自制的style,而不用token;高版本用自制的style也必须用token。
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当你使用`mapboxgl`库中的`source.setTiles`方法更新瓦片后,如果地图图层没有更新,可能的原因有以下几个: 1. 确保你正确地设置了图层的数据源和样式。 在调用`source.setTiles`方法之前,你需要先设置好地图的数据源,并将其与相应的图层关联。确保你已经正确地设置了图层的`source`属性,并且该数据源包含了你更新后的瓦片数据。 2. 确保你设置了适当的瓦片坐标和URL。 在调用`source.setTiles`方法时,你需要提供正确的瓦片坐标和对应的URL。确保你的瓦片坐标与更新后的瓦片数据匹配,并且URL正确指向瓦片数据的位置。 3. 确保你使用了正确的刷新方法。 在调用`source.setTiles`方法后,你需要使用适当的刷新方法来更新地图图层。在`mapboxgl`中,可以使用`map.triggerRepaint()`方法来强制刷新地图,以便更新图层。 以下是一个示例代码,演示了如何使用`source.setTiles`方法更新地图的瓦片,并刷新地图以显示更新后的图层: ```javascript // 设置地图样式和初始数据源 mapboxgl.accessToken = 'your-access-token'; const map = new mapboxgl.Map({ container: 'map', style: 'mapbox://styles/mapbox/streets-v11', center: [lng, lat], zoom: 10 }); map.on('load', () => { // 创建一个新的瓦片源 map.addSource('my-tiles', { type: 'raster', tiles: ['http://example.com/{z}/{x}/{y}.png'], tileSize: 256 }); // 创建一个新的图层 map.addLayer({ id: 'my-layer', type: 'raster', source: 'my-tiles' }); // 更新瓦片数据源 map.getSource('my-tiles').setTiles(['http://example.com/{z}/{x}/{y}.png']); // 刷新地图以显示更新后的图层 map.triggerRepaint(); }); ``` 请注意,上述代码中的URL和其他参数仅作示例用途,你需要根据你自己的数据源和需求进行相应的设置。 希望这个示例能帮助你解决问题!如果你还有其他疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值