Search code examples
javaandroidgoogle-mapsmapboxmapbox-android

Can I inflate a view replacing the icon in mapbox android?


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?
                }

            }

Solution

  • 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