Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change marker color in Google Maps V2

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...

like image 803
Edalol Avatar asked Dec 26 '22 04:12

Edalol


2 Answers

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.

like image 156
Emil Adz Avatar answered Jan 06 '23 06:01

Emil Adz


The following snippet does the trick for me without 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

like image 25
Oliver Avatar answered Jan 06 '23 07:01

Oliver