当前位置: 首页 > news >正文

MapBox Android版开发 4 国际化功能v11

MapBox Android版开发 4 国际化功能v11

  • 前言
  • 遇到的问题
  • 国际化功能
    • 原文给出的方案(V10版)
    • migrate-to-v11
    • 适用于V11版的代码
  • 示例
    • MapStyle类
    • 运行效果图

前言

在前文MapBox地图样式v11中,使用StylelocalizeLabels方法本地化地图语言。但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 class SettingsService. SettingsServiceFactory.getInstance(...) now returns the SettingsService 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影像
在这里插入图片描述在这里插入图片描述

http://www.mrgr.cn/news/22889.html

相关文章:

  • UnLua调用蓝图变量、动画、函数
  • 讨论:无法访问不同网段的Kafka问题
  • Simulink库模块作用及简单应用(一)
  • HarmonyOS学习(九)——窗口管理
  • 最小堆最大堆
  • 如何下载安装AutoCAD 2025
  • 10款国民级企业文件加密系统介绍,究竟哪一个是你的菜?
  • 跨国公司在华研发战略调整对中国IT产业的影响与应对
  • 华为ADS3.0智驾全揭秘,问界M9可以封王了
  • 电脑桌面文件删除了怎么找回来?别急,快速恢复攻略在此
  • HTML5中的数据存储sessionStorage、localStorage
  • 【算法思想·二叉树】后续篇
  • 【 html+css 绚丽Loading 】 000045 太极旋流轮
  • 【算法】分治 · 归并
  • 通过LinkedIn的思想领袖广告提升有机帖子效果,降低每次结果成本
  • 等保测评是什么?为什么要做等保测评?
  • FPGA技术赋能云数据中心:提高性能与效率
  • uniapp中scroll-view标签
  • pyston,一个神奇的 Python 库!
  • 中国企业数据资产入表情况跟踪全文解析