简体   繁体   中英

How to change the google maps marker color to grey in android

Things I already know

  • 1) Create MarkerOptions object.
  • 2) Use object.icon(BitmapDescriptorFactory.defaultMarker(<color value>));
  • 3) The color value is the hue color value. there are some standard color values specified by Google (HUE.BLUE, HUE.AZURE etc)
  • 4) To get the desired color choose a value between 0 and 360. 0 is red, 90 is cyan, 270 is purple

What do I need? This specific float value for grey

My research: Color wheel Grey is not included in HUE colors. It is in Tone. So how to access color values in Tones.

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

}

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

  1. Find one of Google's existing map icons - probably .png.
  2. Get it onto your system and change it using your graphics program ie. GIMP. Do not lose its transparency.
  3. Save it according to Android Studio's naming rules - ie. no hyphens.
  4. In Android Studio, right-click on res/drawable folder.
  5. Click on Show in Explorer.
  6. Go into drawable folder.
  7. 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.
  8. Close Explorer Window.
  9. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM