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

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.mapdemo

import 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.Locale

class 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 tag
                val language = locale.toLanguageTag()
                settingsService.set(MapboxCommonSettings.LANGUAGE, Value(language))

                val worldView = "CN"
                settingsService.set(MapboxCommonSettings.WORLDVIEW, Value(worldView))
            }
        }
    }
}

运行效果图

3D基础3D影像
在这里插入图片描述在这里插入图片描述

http://www.kler.cn/a/302245.html

相关文章:

  • uni-app之数据驱动的picker选择器( uni-data-picker)之可以选择到任意级别
  • 844.比较含退格的字符串
  • OCR识别铁路电子客票
  • MYSQL 库,表 基本操作
  • ML 系列: 第 24 节 — 离散概率分布(泊松分布)
  • IEC60870-5-104 协议源码架构详细分析
  • 什么不建议通过 `Executors` 构建线程池?
  • 抓包工具检测手把手教学 - 某招聘网站
  • 7-6 列出连通集
  • pyqt自定义文本编辑器
  • TCP通信实现
  • 2024 天池云原生编程挑战赛决赛名单公布,9 月 20 日开启终极答辩
  • 【从0开始在CentOS 9中安装redis】
  • Windows编译Hikari-LLVM15[llvm-18.1.8rel]并集成到Android Studio NDK
  • openVX加速-常见问题:适用场景、AI加速、安装方式等
  • 模板(C++)
  • Java中的List与Set转换
  • jantic/DeOldify部署(图片上色)附带Dockerfile和镜像
  • Linux下的系统接口(实时更新)
  • 人工智能安全治理框架导图
  • 【泰克生物】酵母单杂交技术在基因调控研究中的应用
  • 数据结构——查找算法
  • 240908-结合DBGPT与Ollama实现RAG本地知识检索增强
  • OpenCV结构分析与形状描述符(23)确定一个点是否位于多边形内的函数pointPolygonTest()的使用
  • 单链表的查找与长度计算
  • PyCharm与Anaconda超详细安装配置教程