How to change the google maps marker color to grey in android
For those of us who actually want to use the existing hue colors, you can do something like this:
fun MapFragment.addGreenMarker(latLng: LatLng) {
getMap()?.apply {
addMarker(
MarkerOptions()
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
.position(latLng)
.draggable(true)
)
}
}
No grey available as a hue. So one approach is to grab a marker png and modify for grey (externally), as in this snap (that's all the hues plus the special grey one on top). Create BitmapDescriptor from resource:
(For the grey png set transparent color to white.)
Relevant code (not essential for the answer):
public void createMarkers() {
LatLng pos = new LatLng(38.547279, -121.46101);
for (int i = 0; i < 360; i++) {
mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.defaultMarker(i)).position(pos));
double lat = pos.latitude;
double lng = pos.longitude;
lng += 0.01;
if ((i + 1) % 40 == 0) {
lng = -121.46101;
lat = pos.latitude + 0.01;
}
pos = new LatLng(lat,lng);
}
double lat = pos.latitude + 0.01;
pos = new LatLng(lat, pos.longitude);
BitmapDescriptor bd = BitmapDescriptorFactory.fromResource(R.drawable.ic_map_marker);
mMap.addMarker(new MarkerOptions().icon(bd).position(pos));
}
- Find one of Google's existing map icons - probably .png.
- Get it onto your system and change it using your graphics program ie. GIMP. Do not lose its transparency.
- Save it according to Android Studio's naming rules - ie. no hyphens.
- In Android Studio, right-click on res/drawable folder.
- Click on Show in Explorer.
- Go into drawable folder.
- Copy/paste your new icon into res/drawable folder - don't worry about it being a .png file while others in the folder are .xml files.
- Close Explorer Window.
- To use in your app - assumig the file name is gray_dot.png: mMarkerOptions.position(beachCoord).title("marker title").snippet("marker info").icon(BitmapDescriptorFactory.fromResource(R.drawable.gray_dot));
When you are typing R.drawable.gr... - it shows up without the .png extension.
Run your app. Basic testing shows that the icon size works on a tablet and phone just fine without worrying about the different resolutions. I will to more testing to see this remains true for higher resolutions.