18

I'm writing a map-based app on Android using Maps API v2.

I already have markers being placed on the map, and can display custom info windows for those markers, but AFAICT only one info window can be displayed at a time. There are a couple of spots where I want different behaviour: I want to always display the info window for multiple windows, without a marker showing.

I suppose I could write some code to draw the info windows to bitmap-backed canvases and pass those bitmaps to the map as marker "icons". This kind of sums up what I'm trying to do quite well: I want the info windows to be my markers. But this approach would require me to write my own window frame drawing code which I'd rather avoid.

Is there a better way of supporting more than one info window being displayed at once?

Martin
  • 3,703
  • 2
  • 21
  • 43

1 Answers1

23

In the docs it states:

Since there is only one info window shown at any one time, this provider may choose to reuse views, or it may choose to create new views on each method invocation.

So no you can't do it with the regular infoviews but it isn't too hard creating markers that act as infoviews.

Edit

I'd create a view in xml that you want to use as a marker/dialog. Something like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:orientation="vertical"
    android:background="@android:color/white"
    >
    <TextView
        android:text="test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <ImageView 
        android:src="@drawable/ic_launcher"
        android:layout_width="50dp"
        android:layout_height="50dp"/>
</LinearLayout>

Then I'd convert this view into a bitmap and use that bitmap as my marker:

        ImageView image = (ImageView) findViewById(R.id.main_image);

        LinearLayout tv = (LinearLayout) this.getLayoutInflater().inflate(R.layout.test_layout, null, false);
        tv.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight()); 

        tv.setDrawingCacheEnabled(true);
        tv.buildDrawingCache();
        Bitmap bm = tv.getDrawingCache();
Warpzit
  • 27,966
  • 19
  • 103
  • 155
  • @SorinComanescu Well I was going to go with something like this: http://stackoverflow.com/a/3036736/969325 . But it proved to be a little difficult when you inflated your own views. But that would be the path I'd try out. That said, I've never done it and there seems to be no replacement for the old method for google maps (using an itemizedoverlay). – Warpzit Mar 28 '13 at 16:21
  • 2
    @SorinComanescu I managed to figure it out. I've added a simple example with explanation. – Warpzit Mar 28 '13 at 16:35
  • Looks nice, I'll try it tomorrow. Thanks. – Sorin Comanescu Mar 29 '13 at 00:17
  • It worked very well. Tweaked it a bit to make TextView's background translucent and have rounded corners, as documented here: http://androidforums.com/application-development/194627-creating-text-boxes-rounded-edges.html#post1734262 – Sorin Comanescu Mar 29 '13 at 11:22
  • @SorinComanescu Yes thats what I'd do as well :). Glad it worked out. – Warpzit Mar 29 '13 at 12:56
  • This solution is awesome, but expensive when rendering thousands of pins. Is there any other way to achieve this effect? – nukeforum Mar 03 '16 at 16:43
  • @nukeforum when you have that many pins the usual approach is to group them together depending on the zoom level. Anyway I believe there is libraries that can help with whole this process now, so you should search for that as well. – Warpzit Mar 03 '16 at 19:05
  • @Warpzit Thanks, I am using the Google clustering library, I was just looking for more efficient ways to deal with this. Clients asked for the ability to toggle clustering on/off and I'm trying to make label behavior uniform between the two without taxing older devices too hard. I've done a lot of research over the past week+ on this topic and this approach is the best I could find. I ended up restricting the display of labels to a specific zoom level to prevent from having to draw thousands of vehicles this way. I was just curious about other approaches you might be aware of. – nukeforum Mar 03 '16 at 19:10
  • @Warpzit, it works great. If anyone can help me to understand how to capture onclick on the above linearlayout will be appreciated. – Sam Reyes Oct 21 '19 at 16:50
  • @SamReyes It's basically just a big marker. Look into how you handle clicks on the markers and you have your solution. – Warpzit Oct 22 '19 at 07:25
  • @Warpzit thanks for your response. It can be done via a listener method of map api onMarkerclick – Sam Reyes Oct 23 '19 at 01:04