MapBox Android版开发 4 国际化功能v11
- 前言
- 遇到的问题
- 国际化功能
- 原文给出的方案(V10版)
- migrate-to-v11
- 适用于V11版的代码
- 示例
- MapStyle类
- 运行效果图
前言
在前文MapBox地图样式v11中,使用Style
的localizeLabels
方法本地化地图语言。但Mapbox Standard
样式和Mapbox Standard Satellite
样式仍显示英文。本文将介绍MapBox
国际化功能的使用方法。
遇到的问题
在前文本地化样式时,使用了localizeLabels
方法,源码如下:
fun MapboxStyleManager.localizeLabels(locale: Locale, layerIds: List<String>? = null) {if (styleURI == "mapbox://styles/mapbox/standard") {throw RuntimeException("Mapbox Standard style does not support client-side runtime localization." +" Consider using Mapbox internationalization capability instead: https://www.mapbox.com/blog/maps-internationalization-34-languages")}setMapLanguage(locale, this, layerIds)
}
通过源码可以看出,若尝试切换Mapbox Standard
地图语言时,SDK会抛出异常,并给出提示:不支持客户端运行时本地化,考虑使用Mapbox 国际化功能……
Mapbox Standard style does not support client-side runtime localization.
Consider using Mapbox internationalization capability …
那么MapBox推荐的国际化功能是什么?又如何实现Mapbox Standard
样式地图语言的切换?从异常给出的链接可以找到答案。
国际化功能
Dynamically Localize Your Maps with Our New Internationalization Capability(2022),摘取部分原文:
-
Mapbox Internationalization Makes Localizing Maps Easy.
-
No more building and maintaining hundreds of styles.
-
With a more finite number of styles, the map loads dynamically and more quickly based on the users device or browser.
-
Rendering is dynamic and fast based on preferences in the device or browser, so users do not need to manually select a language display on the map.
原文给出的方案(V10版)
// Create a settings service instance as PERSISTENT, the
// language set will be persisted across the application lifecycle.
private val settingsService: SettingsServiceInterface by lazy {SettingsServiceFactory.getInstance(SettingsServiceStorageType.PERSISTENT)
}
val locale = context.getResources().getConfiguration().locale
// set language in bcp-47 tag
val language = locale.language().toLanguageTag();
settingsService.set(MapboxCommonSettings.LANGUAGE, Value(language))// set worldview
val worldView = "US"
settingsService.set(MapboxCommonSettings.WORLDVIEW, Value(worldView))
migrate-to-v11
在V11版中SettingsServiceInterface
没有定义,参考官网migrate-to-v11中的说明:
The interface
SettingsServiceInterface
has been removed in favor of classSettingsService
.SettingsServiceFactory.getInstance(...)
now returns theSettingsService
class.
适用于V11版的代码
// Create a settings service instance as PERSISTENT,
// the language set will be persisted across the application lifecycle.
private val settingsService: SettingsService by lazy {SettingsServiceFactory.getInstance(SettingsServiceStorageType.PERSISTENT)
}
val locale = context.resources.configuration.locales[0]
// set language in bcp-47 tag
val language = locale.toLanguageTag()
settingsService.set(MapboxCommonSettings.LANGUAGE, Value(language))// set worldview
val worldView = "CN"
settingsService.set(MapboxCommonSettings.WORLDVIEW, Value(worldView))
// worldview: CN (China), IN (India), JP (Japan), US (United States)
示例
MapStyle类
修改Style的本地化示例代码
package com.example.mapdemoimport android.content.Context
import com.mapbox.bindgen.Value
import com.mapbox.common.MapboxCommonSettings
import com.mapbox.common.SettingsService
import com.mapbox.common.SettingsServiceFactory
import com.mapbox.common.SettingsServiceStorageType
import com.mapbox.maps.MapboxMap
import com.mapbox.maps.Style
import com.mapbox.maps.extension.localization.localizeLabels
import java.util.Localeclass MapStyle(map: MapboxMap) {private var map = map// Create a settings service instance as PERSISTENT,// the language set will be persisted across the application lifecycle.private val settingsService: SettingsService by lazy {SettingsServiceFactory.getInstance(SettingsServiceStorageType.PERSISTENT)}fun changeStyle(context: Context, style: String) {map.loadStyle(style) {if (style != Style.STANDARD && style != Style.STANDARD_SATELLITE) {it.localizeLabels(Locale.CHINESE)} else {val locale = context.resources.configuration.locales[0]// set language in bcp-47 tagval language = locale.toLanguageTag()settingsService.set(MapboxCommonSettings.LANGUAGE, Value(language))val worldView = "CN"settingsService.set(MapboxCommonSettings.WORLDVIEW, Value(worldView))}}}
}
运行效果图
3D基础 | 3D影像 |
---|---|