I am using android google maps v2 and I am creating map markers and assigning a bitmap. I am getting the bitmap from my assets directory. The reason I get them from assets is that the path I am using to find the 'right' icon is dynamic.
My question is should I be saving the underlying bitmap so that I can reuse it again and again. I read: http://developer.android.com/training/displaying-bitmaps/manage-memory.html
but frankly a lot of there really big sample project was over my head.
Should I be recreating the bitmap each time I need to pass that to a marker for creation, or should I read each bitmap( up to a limit) into memory and save them for reuse.
Each will be drawn on the map regardless so I am not sure I can reuse anyway.
Example: I have 300 markers on the map, with 20 different possible bitmaps. I.E. about 15% are the same marker icon.
For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.
2048 characters in URL is just under 100 GeoCode values. So, again no more than 100 markers.
Since bitmap decoding is quite expensive operation I would definitely recommend to go with bitmap caching. Especially in your case when the number of unique bitmaps is much smaller than number of markers on a map. Simple [LruCache][1]
- based in-memory cache would work just fine. You just need to be careful about how much memory do you use for your cache.
Here is a good example of how to implement in-memory cache: http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
Follow up
Just thought about this a bit more and you can do even better job here. Instead of caching bitmaps you can cache BitmapDescriptor
for every unique bitmap you have. This way you can get some extra performance by avoiding making calls to BitmapDescriptorFactory
every time you need to create a marker
UPDATE
Here is what I mean:
LruCache<String, BitmapDescriptor> cache;
private void initCache()
{
//Use 1/8 of available memory
cache = new LruCache<String, BitmapDescriptor>((int)(Runtime.getRuntime().maxMemory() / 1024 / 8));
}
private void addMarker(LatLng position, String assetPath)
{
MarkerOptions opts = new MarkerOptions();
opts.icon(getBitmapDescriptor(assetPath));
opts.position(position);
mMap.addMarker(opts);
}
private BitmapDescriptor getBitmapDescriptor(String path) {
BitmapDescriptor result = cache.get(path);
if (result == null) {
result = BitmapDescriptorFactory.fromAsset(path);
cache.put(path, result);
}
return result;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With