Set map marker to a custom color Android
Asked Answered
R

2

8

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:)

Ramsey answered 27/10, 2014 at 13:50 Comment(2)
Really a good question, but it seems the solution is to design the marker image and use it.Heap
Okay thanks. I'll just do thatRamsey
A
7

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.

Aged answered 5/12, 2014 at 18:6 Comment(2)
when I am using the converter to get the HUE value for my color (#678E00), it gives me 3 values, (76,100,27.8), but I can only provide 1 number to the BitmapDescriptorFactory. Do you know how I could do this?Sacculate
In your case, you need to convert HEX to HUE e.g using this tool. What seems strange to me is that I got different values (103,142,0) for #678E00Aged
U
1

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]);
}
Undressed answered 23/4, 2019 at 5:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.