简体   繁体   中英

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.

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").

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 . The initSpaceStationSymbolLayer helper method specifically takes care of this:

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. 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. 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 using SymbolOptions#withIconImage and ID_ICON_AIRPORT passed as the argument. You use this same approach for adding your own drawable image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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