Java – Change marker color in Google Maps V2

androidgoogle-mapsgoogle-maps-android-api-2google-maps-markersjava

I'm trying to change the color of a marker.

I have this:

private void addMarker(GoogleMap map, double lat, double lon,
                         int title, int snippet) {
    map.addMarker(new MarkerOptions().position(new LatLng(lat, lon))
                                     .title(getString(title))
                                     .snippet(getString(snippet)));

and then this to add a marker:

addMarker(map, 40.748963847316034, -73.96807193756104,
                R.string.title, R.string.snippet);

I want to change the color of the marker and I thought it would be easy and just implement it like this:

private void addMarker(GoogleMap map, double lat, double lon,
                         int title, int snippet, int icon) {
    map.addMarker(new MarkerOptions().position(new LatLng(lat, lon))
                                     .title(getString(title))
                                     .snippet(getString(snippet))
                                         .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.(getString(icon)));

and:

addMarker(map, 40.748963847316034, -73.96807193756104,
                R.string.title, R.string.snippet, HUE_AZURE);

But I can't use "getString" together with ".icon" apparently.

How can I do this?

Also, is that method of changing color supported for API 8+? I've had lots of problems supporting API 8+ and it would suck if this broke something…

Best Solution

here is a loop where I set markers with different colors on the map and it works great for me:

for (Task tempTask : TasksListAppObj.getInstance().tasksRepository.getTasksRepository())
                {
                    LatLng latlng = new LatLng(tempTask.getLatitude(), tempTask.getLongtitude());
                    if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_WAITING))
                    {
                        newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_blue)));
                    }
                    else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_IN_PROGRESS))
                    {
                        newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_bordo)));
                    }
                    else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_ON_THE_WAY))
                    {
                        newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_turkiz)));
                    }
                    else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_COMPLETE))
                    {
                        newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_orange)));
                    }
                    else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_FAILED))
                    {
                        newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_purpul)));
                    }
}

See if it's helps you.

The if statements are for changing the marker icon.

Related Question