首页 > 其他分享 >MapBox Android版开发 4 国际化功能v11

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

时间:2024-09-09 22:53:11浏览次数:12  
标签:set language val v11 mapbox MapBox import Android com

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

前言

在前文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影像
在这里插入图片描述在这里插入图片描述

标签:set,language,val,v11,mapbox,MapBox,import,Android,com
From: https://blog.csdn.net/kikikiki001/article/details/142060494

相关文章

  • Android视频编辑:利用FFmpeg实现高级功能
    在移动设备上进行视频编辑的需求日益增长,用户期望能够在智能手机或平板电脑上轻松地编辑视频,以满足社交媒体分享或个人存档的需求。Android平台因其广泛的用户基础和开放的生态系统,成为视频编辑应用的理想选择。FFmpeg,作为一个强大的多媒体框架,为Android视频编辑应用提供了......
  • 在 Android 应用中使用 VideoView 播放视频的示例
    这段代码片段是在Android应用中使用VideoView播放视频的示例。下面是对代码的详细解析:代码解析findViewById<VideoView>(R.id.vv).apply{setVideoURI("${baseURL}VideoSrc/${o.getString("Src")}".toUri())start()}查找​VideoView​控件:findViewById<Vi......
  • Flutter 3.24 构建 release 抛出部分依赖 AAPT: error: resource android:attr/lStar
    问题截图:一些讨论:https://github.com/transistorsoft/flutter_background_fetch/issues/369问题原因及解决方案:@Aziz-T该问题与插件的compileSdkVersion和targetSdkVersion有关。出现该问题的原因是部分插件的compileSdkVersion和targetSdkVersion版本过旧。请前往......
  • Android
    在res文件内新建XML->LayoutXMLgotoXML编辑代码基本元素```kotlinandroid:layout_width="match_parent"//尺寸和父容器相适应android:layout_height="wrap_content"//与输入内容相适应android:orientation="vertical"//设置控件排布水平/垂直android:gravity="cente......
  • androidstudio报错devicemanager出错问题
    2024-09-0911:01:57,029[1446798]WARN-Emulator:Pixel8ProAPI35-Failedtoprocess.inifileC:\Users\钁f旦.android\avd<build>.iniforreading.如如何解决1.查日志C:\Users\董浩\AppData\Local\Google\AndroidStudio2024.1\log这个是默认位置我的错误是202......
  • adb命令控质android手机旋转屏幕
    实现adb命令控制Android手机旋转屏幕1.流程概述下面是实现adb命令控制Android手机旋转屏幕的整体流程:步骤操作步骤1连接手机到电脑步骤2启动adb命令行工具步骤3执行adb命令控制手机旋转屏幕2.操作步骤步骤1:连接手机到电脑首先,将手机通过USB线缆......
  • Android开发 - Map 键值对链表的使用解析
    创建和初始化MapHashMap:常用的实现类,基于哈希表Map<String,Integer>map=newHashMap<>();LinkedHashMap:保持插入顺序的实现类Map<String,Integer>map=newLinkedHashMap<>();TreeMap:基于红黑树,按键的自然顺序或提供的比较器排序Map<String,Integer>map=......
  • Android10源码刷入Pixel2以及整合GMS
    一、ASOP源码下载具体可以参考我之前发布的文章二、下载相关驱动包这一步很关键,关系到编译后的镜像能否刷入后运行下载链接:Nexus和Pixel设备的驱动程序二进制文件如下图所示,将两个驱动程序上传到Ubuntu服务器,并进行解压,得到两个脚本:下载解压后会有两个脚本文件,运......
  • android中的render线程是什么?
    在Android中,Render线程(也称为渲染线程)是一个专门用于处理UI绘制和动画的线程。它的主要职责是确保用户界面在屏幕上流畅且高效地呈现。这个线程的引入是为了将渲染任务从主线程(UI线程)中分离出来,以避免主线程因过多任务阻塞而导致界面卡顿或无响应的情况。Render线程的主要......
  • Android中VSYNC代表什么
    在Android中,VSYNC(VerticalSynchronization)是一个垂直同步信号,用于协调显示刷新和绘图操作。VSYNC信号的主要作用是控制屏幕刷新频率与图形渲染的同步,以确保画面显示平滑且没有撕裂现象。VSYNC的工作原理显示刷新周期:屏幕的刷新率(通常为60Hz)决定了每秒可以更新的帧数。每......