9

In the original version of the Android Google Maps API it was very easy to implement an overlay with the following code:

List<Overlay> mapOverlays = mapView.getOverlays();
AlarmOverlay alarmOverlay = new AlarmOverlay();
mapOverlays.add(alarmOverlay);

...it was then possible to override the overlays draw() method and paint vector graphics, override the overlays onTouchEvent() method and give custom controls, etc.

I am at a loss as to how to architect similar custom controls in v2, as the use-case for overlays is not mentioned in the API reference (and markers and polygons are not enough). Does anyone have a suggested way of implementing in v2?

gsysko
  • 988
  • 1
  • 8
  • 19
  • 1
    AFAIK, you now just layer something over the map (e.g., some other `View` in the same `FrameLayout` as the map) and draw in it, using the `Projection` you obtain from `getProjection()` on `GoogleMap`. – CommonsWare Dec 31 '12 at 20:00
  • Thank you! I got the impression this was the case, from reading a [post by Cyril Mottier](http://android.cyrilmottier.com/?p=855), but wanted to be sure before going too far down that path. Thanks for your confirmation. (BTW, managing such an overlay could be a great topic for your ominbus examples ;-) ) – gsysko Dec 31 '12 at 20:11
  • Yeah, in particular, figuring out how to have an overlay like this without interfering with touch events has the potential to be... unpleasant. Personally, the built-in marker/polyline/polygon stuff should more than fill my needs, and there's also the ground-tile stuff that I haven't poked at yet, so hopefully relatively few people have to fuss with full-on overlay equivalents. I'll add it to my to-do list, but it'll likely be a while before I get to it. – CommonsWare Dec 31 '12 at 20:47
  • I can appreciate the limited use case. Unfortunately I must go down this path for my app (for various reasons - vector painting, more complex onTouchEvents, etc.). So far, I have run into the problem that the onCameraChangeListener is really not responsive enough to keep my layers moving in sync - very messy! – gsysko Jan 03 '13 at 20:45

1 Answers1

3

If you need to place your own image on the surface of the earth, use GroundOverlay. The method addGroundOverlay adds such image. It takes GroundOverlayOptions that allow to specify the image size and location (in lat long terms) and also BitmapDescriptor that, among other options, can use the ordinary Bitmap as the image source. So you can create a new Bitmap, create Canvas around this bitmap and paint there.

Seems a good approach if you need to draw something really complex, something for that polygons and markers are not sufficient. Also, the old code that draws on Canvas probably can be reused.

I have not done enough testing how soon the map will be updated after we update the bitmap. In the worst case, maybe the ground overlay needs to be removed and the new ground overlay added. The bitmap itself probably still can be reused.

Putting additional component on the top may be problematic as it must support zooming, moving around and the map is now even 3D.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
  • Thanks for your comment. I think you are right. I have not had time to confirm the implementation you suggest, but I will try it. – gsysko Feb 03 '13 at 17:52
  • Regardless, you are exactly right about the issues encountered when overlaying a view on top of the map. It works fairly well, but is not quite performant enough and does not work well with camera animations (such as when zooming). Touch events (as in moving) can be handled by overriding `onInterceptTouchEvent` in the viewgroup holding the map, but then flings are not accounted for. Thank you very much for the advice. – gsysko Feb 03 '13 at 17:59