Monday 14 September 2015

Always Visible Multiple InfoWindows on Google Map



This post is about resolution of an issue that I faced when I started working on a requirement in which multiple info windows were supposed to be shown and that too simultaneously.

Conventionally there is no such method/class in Google maps api that could allow developers to implement this functionality. Though we can add multiple markers default or customized with an attached infowindow but cannot show all the infowindow simultaneously.


Prerequisite : 
Get google map key for your map to be launched correctly. Please following link to create one
https://developers.google.com/maps/documentation/android-api/signup


Alternative:
Whatever. Above feature is highly needed along with autoupdating the content on the info window.
Never knew Markers were enough to replicate it into an infowindow too.



Conception: 
We will be going to create custom Markers in which we will draw all the information as bitmap and then set it to the markers icon property using

customMarkerOption.icon(BitmapDescriptorFactory.fromBitmap(createMarkerBitmap("76.3, 23.4 75%"))));


where customMarkerOption is an instance of MarkerOptions and createMarkerBitmap() is the method that creates bitmap using the content to be shown on map.



private Bitmap createMarkerBitmap(String iMarkerText) {
         Bitmap.Config conf = Bitmap.Config.ARGB_8888;
         Bitmap bmp = Bitmap.createBitmap(200, 100, conf);
         Canvas canvas = new Canvas(bmp);
 
         canvas.drawRect(0,75,200,175, mMarkerContainerPaint);
         canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.purple_marker),100,25,mMarkerTextPaint);
         canvas.drawText(iMarkerText, 50, 100, mMarkerTextPaint);
 
         return bmp;
     }


You can set up the map and use MarkerOptions instances above to create markers on map. And the content will also update automatically.

You can get the complete source for the demo app here.
MapDemos


Please check and let me know in case of issues.