简体   繁体   中英

Android Google map: Change color on map marker

Long story short; I got at Bluetooth device measuring signal-strength of other devices in the area, and then I log the GPS positions on the Android app.

I place the positions on a Google map in my app, but I would like the marker to change color depending on the value of signal-strength (0-255) from red to green or something like that.

I think I need to make my own Drawable in the app or change what is in the:

Drawable drawable = this.getResources().getDrawable(R.drawable.marker);

But I can't find a way draw the markers in the android app.

Any ideas?

Are you using a GoogleMap ? If you have the Drawable-generation down, all you have to do is hook it up via the MarkerOptions.icon method:

BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.resource);
MarkerOptions opts = new MarkerOptions().icon(descriptor);
this.getMap().addMarker(opts);

This will draw your drawable on the map.

You can replace fromResource with fromBitmap if needed.

I think you have to try similar code below , but it is applicable for default marker only , colors will change depending on hue value , this is snippet from Google Maps API Demos :

    // Creates a marker rainbow demonstrating how to create default marker icons of different
    // hues (colors).
    int numMarkersInRainbow = 12;
    for (int i = 0; i < numMarkersInRainbow; i++) {
        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(
                        -30 + 10 * Math.sin(i * Math.PI / (numMarkersInRainbow - 1)),
                        135 - 10 * Math.cos(i * Math.PI / (numMarkersInRainbow - 1))))
                .title("Marker " + i)
                .icon(BitmapDescriptorFactory.defaultMarker(i * 360 / numMarkersInRainbow)));
    }

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