如何在Mapbox GL JS中使用自定义字体?
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要设置样式的
字形
属性:更多信息: https://docs.mapbox.com/mapbox-gl-js/style-spec/glyphs/
You need to set the
glyphs
property of your style:More info: https://docs.mapbox.com/mapbox-gl-js/style-spec/glyphs/