Key art image for MapView with Widgets Android

MapView with Widgets Android

This phased me for quite sometime, how do I use a widget like a button, or a view as an overlay?

I asked on the Google Groups, got a great reply from Mark Murphy, and he explained about how you can use relative layouts to add views on top of the map view and set x, y coordinates and use z ordering.

I started to think about what the MapView was, then a quick look at the API and bam, addView.

So here’s how you add a view to a MapView and I’m sure you can add buttons to it etc

LinearLayout v = (LinearLayout) View.inflate(getApplicationContext(), R.layout.markerlayout, null);
TextView markerTextView = (TextView) v.findViewById(R.id.MarkerTextView);
markerTextView.setText("Example Text");
_mapView.addView(v, 0,
  new MapView.LayoutParams(
    ViewGroup.LayoutParams.WRAP_CONTENT,
    ViewGroup.LayoutParams.WRAP_CONTENT,
    new GeoPoint((int) (latitudeVariable * 1E6), (int) (longitudeVariable * 1E6)),
    MapView.LayoutParams.BOTTOM_CENTER
  )
);

The LayoutParams is the key to defining where the marker view is kept in relation to the map.

Enjoy!

Orig. Photo: https://flic.kr/p/5DiwQV

Found an issue?

All my posts are available to edit on GitHub, any fix is greatly appreciated!

Edit on GitHub