22

I can't beleive there's no easy way to do such a basic thing like this... I want to show a popup/baloon (a LinearLayout) after user clicks on a map marker (something smilar to what is in Google Maps app). It should move with the map, when the user scrolls the map. What is the best way to do this?

One idea is to have the LinearLayout in my Activity's root layout and show it when needed. But how to make it move with the map?

Another way to do that may be to create an Overlay that draws the LinearLayout in onDraw and gives the layout touch events. Is this possible?

fhucho
  • 34,062
  • 40
  • 136
  • 186

2 Answers2

46

The way I did is:

Put the markers at required GeoPoints by subclassing ItemizedOverlay, as described in http://developer.android.com/guide/tutorials/views/hello-mapview.html

Create a popup View by inflating from the layout:

View popUp = getLayoutInflater().inflate(R.layout.map_popup, map, false);

Use MapView.LayoutParams to position the popup with respect to GeoPoint in the ItemizedOverlay< OverlayItem >::onTap method. Popup will scroll automatically (without any additional code) when user scrolls the map. Basically popup gets tied to a GeoPoint, if user zooms, popup's position gets adjusted automatically.

MapView map = (MapView) findViewById(R.id.mapview);   
MapView.LayoutParams mapParams = new MapView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        <geopoint>,
                        <x offset if required>,
                        <y offset like pinHeight>,
                        MapView.LayoutParams.BOTTOM_CENTER);
map.addView(popUp, mapParams);
Kiran
  • 814
  • 9
  • 6
  • nothing happens when I add the popup view. It doesn't appear? Have you encountered this? – Andreas Rudolph Oct 28 '12 at 22:47
  • Figured it out! Remember kids, always put another view inside the top viewgroup of your popUp-view! Because that viewgroup (the root-one you need to put content inside of) will get the layoutparams from the mapParams. Maybe this comment will help someone someday. – Andreas Rudolph Oct 29 '12 at 18:10
  • @OneManMobile maybe this comment would help me if I understood it. Maybe you could look at my [issue](http://stackoverflow.com/questions/13271458/add-legenda-to-osmdroid-mapview-with-pop-up) and tell me what exactly another view inside the top viewgroup of your popUp-view would be? – birgit Nov 07 '12 at 21:47
12

Here is the "the missing Widget"...

OneWorld
  • 17,512
  • 21
  • 86
  • 136
  • Not ideal solution (that I am using) is to have the popup inside the root RelativeLayout above the map and change its position when user scrolls the map. It's not ideal, because the popup is not moving at the same speed as the map - it's lagging a bit behind the map, when I move the map. When I have time I'll try to manually draw the popup layout in custom overlay. – fhucho Oct 07 '10 at 14:21
  • Is that Kiran's solution u are using? – OneWorld Oct 07 '10 at 16:29
  • Your solution really sounds just like a work around. I also thought of creating a secondary marker right above the first marker. But the more I think about it, I recognize that its not really good to do it that way. By the way, we are talking about the same issue here: http://stackoverflow.com/questions/3880623/how-to-show-a-balloon-above-a-marker-in-a-mapactivity-is-there-an-api-to-do-that We can merge our questions some time – OneWorld Oct 07 '10 at 17:11
  • Hi its not recognizing BalloonItemizedOverlay why..? I am using API level 8 is thats the problem...? – Noby Sep 07 '11 at 12:33