Marker color change in Google Maps V2

I am trying to change the color of a marker.

I have it:

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 add the 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 simple to 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" with ".icon", apparently.

How can i do this?

Also, is this color change method supported for API 8+? I had a lot of problems with API 8+ support, and it would suck if it broke something ...

+4
source share
2 answers

here is the loop where I set markers with different colors on the map, and it works fine 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 this helps you.

If statements are used to change the marker icon.

+5
source

The following snippet does the trick for me with no dependencies:

 BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE); myMap.addMarker(new MarkerOptions() .position(point) .icon(bitmapDescriptor) .title(point.toString())); 

Found it here: http://android-er.blogspot.de/2013/01/change-marker-color-of-googlemaps-v2.html

+4
source

Source: https://habr.com/ru/post/1479743/


All Articles