Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom markers showing with white color on some devices (Google Maps v3 Android SDK)

I have Google Maps SDK running with custom markers on an android app. Everything works fine on our test phones -- an A7000, an Samsung and other. However, when I run the app on Nexus 5 or LG devices, one type of custom marker displaying white on the map.

While the markers are showing white,will perform desired behavior.

We're super confused about this, especially because another custom marker (that is very similar) works fine. Other than hardware, the only other difference between our test phones and the Nexus 5 is that the Nexus is running Android 6.0, vs 5.x and 4.x for our test phones.

Code using to add marker

for(i=0;i<latLngs.size;i++)
{
LatLng latLng=latLngs.get(i);
Marker m = mMap.addMarker(new MarkerOptions().position(latLng).title("Title").icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
}

for change the marker icon using

 m.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.marker1));

On nexus device google map appear like below, some off the markers become white ,i am using loop to add markers.

enter image description here

like image 612
Om Infowave Developers Avatar asked May 16 '16 10:05

Om Infowave Developers


People also ask

How do you change the color of a marker on Google Maps?

To edit the marker color, click or tap on the marker icon. When you do that, you can change both the color of the marker and its style. Go for the color or style you want to change and then click OK to see the effect. Click on the button labeled Done to save your new marker color settings.

What do the different color markers mean on Google Maps?

Food and drink is now colored orange; shopping remains blue; pink is health; seafoam green is entertainment and leisure; green is for outdoor; and lighter blue is for transport.


1 Answers

EDIT: With version 9.2.56 of the Google Play Services app released on June 13th, the bug has been fixed.

I found a workaround to your problem, however it is quite ugly and it's probably not advised to use it unless you know you're going to use a little number of markers.

Instead of this:

//Getting a reference to your activity's resources
final Resources resources = myActivity.getResources();
//Defining your drawable res id
final int resId = R.drawable.my_drawable_res_id;
marker.setIcon(BitmapDescriptorFactory.fromResource(resId));

Do this:

//Getting a reference to your activity's resources
final Resources resources = myActivity.getResources();
//Defining your drawable res id
final int resId = R.drawable.my_drawable_res_id;
marker.setIcon(
    BitmapDescriptorFactory.fromBitmap(
        BitmapFactory.decodeResource(resources, resId)));

Workaround found after reading this link (thanks @antonio in your comments): https://code.google.com/p/gmaps-api-issues/issues/detail?id=9765

The bug is caused on certain devices with a recent version of the Google Play Services library (probably 8.7+ or 9+).

It happens if you share a BitmapDescriptor with multiple markers, so the workaround is to re-create one every time. I think BitmapDescriptorFactory.fromResource might cache read resources somehow, that's why you need to decode it as a bitmap.

Here is the quote of the #10 reply of transbao at the link antonio gave:

We can repro this bug which causes certain marker icons to render as white patches. Your app may be affected if an icon bitmap is shared among multiple markers, although the issue only manifests in specific usage scenarios.

In the short term, we'd recommend the workaround in #8 -- use a unique Bitmap object for each marker:

marker.setIcon(BitmapDescriptorFactory.fromBitmap( BitmapFactory.decodeResource(getResources(), R.drawable.drawableid)));

and

new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap( BitmapFactory.decodeResource(getResources(), R.drawable.drawableid)));

Creating a BitmapDescriptor once and reusing it won’t be sufficient. E.g. if you’re doing:

BitmapDescriptor bd = ...; marker1.setIcon(bd); marker2.setIcon(bd);

...then the workaround would be:

marker1.setIcon(BitmapDescriptorFactory.fromBitmap( BitmapFactory.decodeResource(getResources(), R.drawable.drawableid))); marker2.setIcon(BitmapDescriptorFactory.fromBitmap( BitmapFactory.decodeResource(getResources(), R.drawable.drawableid)));

Please note that, if your app uses a lot of markers, this workaround could possibly result in higher memory consumption. Also, unfortunately the workaround doesn’t apply when the default icon is used via BitmapDescriptorFactory.defaultMarker().

like image 144
androidseb Avatar answered Sep 30 '22 19:09

androidseb