Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show popup above map marker in MapView

Tags:

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?

like image 759
fhucho Avatar asked Sep 14 '10 10:09

fhucho


People also ask

How to Add InfoWindow on marker Android?

The simplest way to add an info window is to set the title() and snippet() methods of the corresponding marker. Setting these properties will cause an info window to appear whenever that marker is clicked.

What is the use of addMarker() in Maps?

addMarker(markerOptions) method. Markers are designed to be interactive. They receive click events by default, and are often used with event listeners to bring up info windows. Setting a marker's draggable property to true allows the user to change the position of the marker.

How to drag marker on Google Maps in Android?

Setting draggable to true makes the marker draggable and vice versa is true. Now with this, after the marker is shown, you can long-press the marker then drag it to your preferred location. Also, note that this is possible because if you can see on the map initialization we added a marker drag listener.


1 Answers

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); 
like image 130
Kiran Avatar answered Oct 01 '22 19:10

Kiran