Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapbox for Android: Changing color of a marker's icon

Tags:

android

mapbox

I'm using Mapbox in my Android app and have custom (black) icons for my markers. Is it possible to change a marker's icon color to something else if it's black?

I can also use a default icon if this is possible with default icons.

like image 288
Anna Evtushenko Avatar asked Jun 14 '16 07:06

Anna Evtushenko


Video Answer


2 Answers

Mapbox Android SDK uses a PNG file for the marker icon. You can use any PNG file as an icon, in other words the marker can be just about any color you want. Heres an example with a marker using a custom icon. Heres the code doing most the work:

            // Create an Icon object for the marker to use
            IconFactory iconFactory = IconFactory.getInstance(MainActivity.this);
            Drawable iconDrawable = ContextCompat.getDrawable(MainActivity.this, R.drawable.purple_marker);
            Icon icon = iconFactory.fromDrawable(iconDrawable);

            // Add the custom icon marker to the map
            mapboxMap.addMarker(new MarkerOptions()
                    .position(new LatLng(-33.8500000, 18.4158234))
                    .title("Cape Town Harbour")
                    .snippet("One of the busiest ports in South Africa")
                    .icon(icon));

Here are some markers I made using the same style as the default icon:

Place these in your projects drawable-xxhdpi folder. These are pulled from the Mapbox Android Demo app you can get from the play store

like image 84
cammace Avatar answered Oct 29 '22 17:10

cammace


I use this

 map.addMarker(new MarkerOptions()
            .position(new LatLng(lat,lng))
            .icon(IconFactory.getInstance(this).fromResource(R.drawable.ic_marker))
    );
like image 41
Radesh Avatar answered Oct 29 '22 17:10

Radesh