2

When adding markers in mapbox, there is a provision to add custom icon to the marker. I wonder whether we can inflate a view(R.layout file) instead of assigning a drawable icon.

Here is the code:-

 public void onMapReady(MapboxMap mapboxMap) {
                IconFactory iconFactory=IconFactory.getInstance(context);
                for(int i=0;i<coordinates.size();i++){
                    mapboxMap.addMarker(new MarkerOptions()
                    .position(new LatLng(lat,longt))
                    .icon(iconFactory.fromResource(R.drawable.ic_location_green))
                          //can we inflate a view here instead of assigning a drawable image?
                }

            }
Aadhil Ahmed
  • 111
  • 15

2 Answers2

3

I don't think that it is possible

What you can do is draw custom icon at runtime:

  1. Draw it on Canvas

  2. Generate Drawable at runtime (instead of creating xml, you can create an object. So if you were to type <shape>, you can replace it with new Shape(); in Java)

  3. Generate a view and copy its bitmap (How to convert Views to bitmaps?) this option would provive only looks of it - things like click listeners will not work, thus I don't see a reason for choosing this option

AndroidGuy
  • 52
  • 7
  • I just want to show some dynamic data inside the generated drawable view? Is it possible? – Aadhil Ahmed Nov 03 '17 at 13:51
  • In that case, look for for some way to get/find that marker by position and use .icon to replace it if such method does not exist (and i'm quite sure that it does) - you can use 'reflection' to get reference to markers list/hashmap –  Nov 03 '17 at 13:58
1

This possible using the following utility class:

  /**
   * Utility class to generate Bitmaps for Symbol.
   * <p>
   * Bitmaps can be added to the map with {@link com.mapbox.mapboxsdk.maps.MapboxMap#addImage(String, Bitmap)}
   * </p>
   */
  private static class SymbolGenerator {

    /**
     * Generate a Bitmap from an Android SDK View.
     *
     * @param view the View to be drawn to a Bitmap
     * @return the generated bitmap
     */
    public static Bitmap generate(@NonNull View view) {
      int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
      view.measure(measureSpec, measureSpec);

      int measuredWidth = view.getMeasuredWidth();
      int measuredHeight = view.getMeasuredHeight();

      view.layout(0, 0, measuredWidth, measuredHeight);
      Bitmap bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888);
      bitmap.eraseColor(Color.TRANSPARENT);
      Canvas canvas = new Canvas(bitmap);
      view.draw(canvas);
      return bitmap;
    }
  }

A full example integrating this code can be found here. Note that this code can be executed on a background thread, so you don't need to block the main thread for it. While we don't expose a binary atm we are looking into creating a little plugin around this code. The feature request for it can be found here.

Tobrun
  • 18,291
  • 10
  • 66
  • 81