如何在Mapbox GL JS中使用自定义字体?

发布于 2025-02-08 11:58:10 字数 1211 浏览 1 评论 0原文

我正在使用Mapbox GL JS(v2.8.0)来编程地向我的地图添加样式层,并且我正在尝试使用一组通过Mapbox Studio上传到地图的字体。

我目前在我的样式层的代码中有此内部:

const layer = {
    id: this.getLayerId(),
    type: 'symbol',
    layout: {
        'text-field': ['get', 'name'],
        'text-font': [
            "Chakra Petch Italic", // My custom font inside Mapbox
            "Arial Unicode MS Regular" // Fallback font
        ]
    },
    paint: {
        'text-color': '#ffffff',
    }
}

但是,在渲染地图时,我会在尝试获取字体方面遇到404个错误。

它正在尝试从以下URL获取字体:

https://api.mapbox.com/fonts/v1/mapbox/Chakra%20Petch%20Italic,Arial%20Unicode%20MS%20Regular/0-255.pbf?access_token={access_token} 

我相信这里的问题是,当我时,它应该是/mapbox/应该是>/{username}/转到浏览器中的以下URL:

https://api.mapbox.com/fonts/v1/{username}/Chakra%20Petch%20Italic,Arial%20Unicode%20MS%20Regular/0-255.pbf?access_token={access_token} 

我正确地获取.pbf数据。

我如何在我的样式层JSON中指定它应该在我的用户名而不是公共Mapbox用户下看?


此外,作为测试,如果我在Mapbox Studio中创建一个简单的图层 - 并设置一个标签以使用自定义字体,在DevTool的“网络”选项卡中,我可以看到它使用上面的用户名URL来获取字体。不幸的是,该标签的工作室中的“代码检查器”显示了与我在代码中相同的两个字符串数组。

I am using Mapbox GL JS (v2.8.0) to programmatically add a Style layer to my map, and I am trying to use a set of fonts that I have uploaded to my map via the Mapbox Studio.

I currently have this inside my code for my Style layer:

const layer = {
    id: this.getLayerId(),
    type: 'symbol',
    layout: {
        'text-field': ['get', 'name'],
        'text-font': [
            "Chakra Petch Italic", // My custom font inside Mapbox
            "Arial Unicode MS Regular" // Fallback font
        ]
    },
    paint: {
        'text-color': '#ffffff',
    }
}

However when it comes to rendering my map, I am getting 404 errors in relation to trying to fetch my font.

It is trying to fetch the font from the following url:

https://api.mapbox.com/fonts/v1/mapbox/Chakra%20Petch%20Italic,Arial%20Unicode%20MS%20Regular/0-255.pbf?access_token={access_token} 

And I believe this issue here is that instead of /mapbox/ it should be /{username}/ as when I go to the following url in the browser:

https://api.mapbox.com/fonts/v1/{username}/Chakra%20Petch%20Italic,Arial%20Unicode%20MS%20Regular/0-255.pbf?access_token={access_token} 

I correctly get the .pbf data.

How am I able to specify that it should look under my username, rather than the public mapbox user, in my style layer json?


Additionally, as a test, if I create a simple layer inside Mapbox Studio - and set a label to use the custom font, in the Network tab in devtools I can see it fetching the font using the username url above. And unfortunately the "code inspector" in the Studio for this label shows the same two string array as I have inside my code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弄潮 2025-02-15 11:58:10

您需要设置样式的字形属性:

   }, glyphs: 'mapbox://fonts/yourusername/{fontstack}/{range}.pbf'
}

更多信息: https://docs.mapbox.com/mapbox-gl-js/style-spec/glyphs/

You need to set the glyphs property of your style:

   }, glyphs: 'mapbox://fonts/yourusername/{fontstack}/{range}.pbf'
}

More info: https://docs.mapbox.com/mapbox-gl-js/style-spec/glyphs/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文