简体   繁体   English

Mapbox Android 中的可绘制资源

[英]Drawable Resource in Mapbox Android

I have some icons as drawable resources that I would like to put in specific locations in a MapBox map in android studio, but I don´t know how.我有一些图标作为可绘制资源,我想将它们放在 android 工作室的 MapBox map 中的特定位置,但我不知道如何。

I have tried converting my resource files into bitmaps, and then convert those bitmaps into strings so as to fill the "withIconImage" method of SymbolOptions class.(I know it works with defined strings such as "airport", "fire-station").我尝试将我的资源文件转换为位图,然后将这些位图转换为字符串,以填充 SymbolOptions class 的“withIconImage”方法。(我知道它适用于定义的字符串,例如“airport”、“fire-station”) .

Can someone help me?有人能帮我吗?

Thank you!谢谢!

This example from the Mapbox Android documentation shows how to add a local drawable resource from an Android application to your Mapbox map as a SymbolLayer . 这个来自 Mapbox Android 文档的示例展示了如何将 Android 应用程序中的本地可绘制资源作为 SymbolLayer 添加到 Mapbox SymbolLayer The initSpaceStationSymbolLayer helper method specifically takes care of this: initSpaceStationSymbolLayer辅助方法专门处理这个问题:

private void initSpaceStationSymbolLayer(@NonNull Style style) {
  style.addImage(
    "space-station-icon-id",
    BitmapFactory.decodeResource(this.getResources(), R.drawable.iss)
  );

  style.addSource(new GeoJsonSource("source-id"));

  style.addLayer(new SymbolLayer("layer-id", "source-id").withProperties(
    iconImage("space-station-icon-id"),
    iconIgnorePlacement(true),
    iconAllowOverlap(true),
    iconSize(.7f)
  ));
}

You mentioned SymbolOptions , however, so it is likely the case that you are using the Mapbox Annotation Plugin for Android rather than directly adding SymbolLayer s.但是,您提到SymbolOptions ,因此很可能您正在使用 Android 的Mapbox 注释插件,而不是直接添加SymbolLayer As indicated in the documentation for the SymbolOptions#withIconImage method, icon images are specified as String s which reference the names of images in your style's sprite sheet.正如SymbolOptions#withIconImage方法的文档中所指出的那样,图标图像被指定为String ,它引用了您样式的sprite表中的图像名称。 This example from the Mapbox Android Plugins demo app demonstrates how to add an image from the resources folder to your style, to then be used as the icon image in a SymbolManager . 这个来自 Mapbox Android 插件演示应用程序的示例演示了如何将资源文件夹中的图像添加到您的样式中,然后用作SymbolManager中的图标图像。 Namely, ID_ICON_AIRPORT is defined as "airport" here , then the helper method addAirplaneImageToStyle here adds the relevant image to the style, and finally a Symbol is created here using SymbolOptions#withIconImage and ID_ICON_AIRPORT passed as the argument.即这里定义了ID_ICON_AIRPORT"airport" ,然后这里的辅助方法addAirplaneImageToStyle将相关图像添加到样式中,最后在这里使用SymbolOptions#withIconImage和作为参数传递的ID_ICON_AIRPORT创建一个Symbol You use this same approach for adding your own drawable image.您使用相同的方法来添加您自己的可绘制图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM