Android : Adding custom marker to Google Map using TextView as Bitmap

on Friday, April 3, 2015


I'm trying to programmatically add labels onto my Google Map using the Google Maps SDK for Android. My proposed solution is to use custom markers where the icon/image is basically a bitmap created from a TextView.


Here's what I have so far:



TextView label = new TextView(Activity.this);
label.setText(name);
label.setHeight(20);
label.setWidth(500);
label.setDrawingCacheEnabled(true);

if(label.getDrawingCache() != null) {
Bitmap labelImage = Bitmap.createBitmap(label.getDrawingCache());
Marker labelMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(averageLatitude, averageLongitude))
.title(name)
.icon(BitmapDescriptorFactory.fromBitmap(labelImage))
);
}


However, label.getDrawingCache() is returning null so it's not able to draw the marker image. Any ideas?


0 comments:

Post a Comment