Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set map marker to a custom color Android

I am making an app that adds pins to a map at certain points. I want the color of my pins to match the theme colors of our app. Sorry I'm really a noob

int color = Color.rgb(255, 201, 14);
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
final LatLng PERTH = new LatLng(40, -80);
Marker perth = mMap.addMarker(new MarkerOptions()
  .position(PERTH)
  .title("MY PIN")
  .snippet("MAGGIE EATS SNAKE SKINS")
  .draggable(true)
  .icon(BitmapDescriptorFactory.fromResource(R.drawable.pin))
  .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.color)));

The .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.color))); does not work. It will not allow me to insert a custom color here. How can I do this? Thanks:)

like image 325
abbyt22 Avatar asked Oct 27 '14 13:10

abbyt22


People also ask

How do you change the color of a marker on a map?

To edit the marker color, click or tap on the marker icon. When you do that, you can change both the color of the marker and its style. Go for the color or style you want to change and then click OK to see the effect. Click on the button labeled Done to save your new marker color settings.


2 Answers

defaultMarker() method allows to set custom color, but only by providing hue value. According to android documentation:

(Hue) Value must be greater or equal to 0 and less than 360

If you know Hex or RGB value of your app theme, you need to do some calculations (see example ) or simply use some free online converter. In your case hue value will be 47.

Also, there is no need to set .icon() property in your code twice.

like image 161
Leszek Jasek Avatar answered Oct 06 '22 12:10

Leszek Jasek


I have created this simple method to get marker of any color.

 public BitmapDescriptor getMarkerIcon(int color) {
    float[] hsv = new float[3];
    Color.colorToHSV(color, hsv);
    return BitmapDescriptorFactory.defaultMarker(hsv[0]);
}
like image 45
hardik kava Avatar answered Oct 06 '22 12:10

hardik kava