Moving icons on a Android MapView
Asked Answered
S

4

11

I'm working on an application that displays the location of moving items on a Google MapView. I need a way to update the position of the icons that represent the items (as well as change the facing of the icons every two seconds as updated data comes in).

I currently have an activity in the app that extends MapActivity. On to this I have overlaid a static Overlay that draws some lines on the map and an ItemizedOverlay that draws a static icon.

There is a draw() method that claims to be used by animated overlays, but overriding it to do my animations still doesn't make anything animate.

Do I need to tell the app to make my overlay animate, or do I need to use a different type of overlay?

Solita answered 25/9, 2010 at 22:32 Comment(0)
G
4

There are two things that I have in mind about your problem.

First, did you consider using invalidate() on the MapView to force a redraw? That's probably not the most efficient solution, but it should at least get you something you can work from.

Second, you can refresh the items on your map by calling the activity again. Here is an example.

Gina answered 2/10, 2010 at 16:49 Comment(3)
I have considered the invalidate option. From what I've read, it can be iffy on performance, but I'll give it a shot. What I wonder is: how do I invalidate the overlay ~20 times per second in order to smoothly animate the icons from place to place?Solita
maybe you should consider using a timer thread, check out this anddev.org/viewtopic.php?t=126Gina
I ended up using a regular View and postInvalidating it from a TimerThread to give me the updates. As it happens, I don't want the map to be redrawn every frame, just the moving objects on this other view, so I'm looking in to use drawing cache to save the MapView. That said, your answer about timer threads was pretty much right. It lets you schedule the refresh for a fixed number of times per second so you can guarantee good framerate without displaying every possible frame.Solita
M
1

Using built-in capabilities of MapView and Overlays to draw animation will likely kill your perfromance. My suggestion is:

1) Combine MapView with another View. MapView will give you necessary screen coordinates and another view will serve as a canvas for animation. Both views should occupy the same screen positions (use frame layout);

2) Once you get positions of the map marker you want to animate - remove it from map and start doing animation of marker's movements using second view. Here you can leverage specialized android animation classes (i.e. Animation, AnimationUtil, AnimationSet)

3) Once you've done with animation - add marker to mapview overlay, now in its new position and invalidate overlay

Maybe there can be more optimal approach (i.e. use animation directly on MapView canvas), but the bottom line - you should use specialized animation mechanism to prevent perfromance issues.

Margaretemargaretha answered 27/10, 2010 at 16:57 Comment(0)
R
0

I'm still pretty new to all this Android Stuff, but I was reading Android Unleashed 2nd ed (manning early access program version) the other day, where it covers Animations... which sounds like just the thing.

Animation anim = new TranslateAnimation( startX, stopX, startY, stopY );
anim.setDuration( 2000 /* 2 seconds */ );
anim.init( overlayWid, overlayHei, parentWid, parentHei )

myItemizedOverlay.startAnimation( anim );

Note that I haven't actually tried anything of the sort, but that's what the book/JavaDoc leads me to believe will work. You will probably need to call prior to startAnimation(). Like I said... hardly my area of expertise (yet!).

I don't think you'll need to mess with invalidate at all with this approach.

Radar answered 20/10, 2010 at 23:57 Comment(1)
I believe you cannot use animations on ItemizedOverlays that way.Bombast
G
0

As option to display animation you can use addView method of MapView class. It is as simple as:

  1. just create a view (like ImageView or any other View);
  2. initialize the view layout, create and pass MapView.LayoutParams to setLayoutParams method;
  3. add the view to MapView.

View will automatically move when you scroll the map.

public static void addAnimationToMap(MapView map, int animationResourceId, GeoPoint geoPoint) {

    final ImageView view = new ImageView(map.getContext());
    view.setImageResource(animationResourceId);

    //Post to start animation because it doesn't start if start() method is called in activity OnCreate method.
    view.post(new Runnable() {
        @Override
        public void run() {
            AnimationDrawable animationDrawable = (AnimationDrawable) view.getDrawable();
            animationDrawable.start();
        }
    });

    map.addView(view);

    MapView.LayoutParams layoutParams = new MapView.LayoutParams(
            MapView.LayoutParams.WRAP_CONTENT,
            MapView.LayoutParams.WRAP_CONTENT,
            geoPoint,
            MapView.LayoutParams.BOTTOM_CENTER);
    view.setLayoutParams(layoutParams);
}
Gearalt answered 5/10, 2011 at 21:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.