Mapbox 中的可绘制资源 Android

Drawable Resource in Mapbox Android

我有一些图标作为可绘制资源,我想将它们放在 android 工作室的 MapBox 地图中的特定位置,但我不知道如何。

我试过将我的资源文件转换成位图,然后将这些位图转换成字符串,以填充 SymbolOptions class 的 "withIconImage" 方法。(我知道它适用于定义的字符串,例如如 "airport", "fire-station").

有人可以帮助我吗?

谢谢!

Mapbox Android 文档中的

This example 展示了如何将来自 Android 应用程序的本地可绘制资源作为 SymbolLayer 添加到您的 Mapbox 地图。 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)
  ));
}

您提到了 SymbolOptions,但是,您使用 Mapbox Annotation Plugin for Android rather than directly adding SymbolLayers. As indicated in the documentation for the SymbolOptions#withIconImage method, icon images are specified as Strings which reference the names of images in your style's sprite sheet. 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. 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 的情况很可能是使用 SymbolOptions#withIconImageID_ICON_AIRPORT 作为参数传递的。您可以使用相同的方法添加自己的可绘制图像。