moveToLatLong Animation

295 views
Skip to first unread message

R Miner

unread,
Dec 6, 2011, 4:49:52 AM12/6/11
to route-me
I am a route-me newbie. I searched quite a bit on this group and the
web in general to see if this question is answered but couldn't find a
solution.

I was able to successfully make mapView move to a given latlong using
move. However, I am not able to make the move animate smoothly (like
in the native maps app).
I was able to get animation itself to work but not in conjunction with
movetolatlong. Here's my code. What am I doing wrong?

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3.0];
[UIView setAnimationDelay:0.2];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDidStopSelector:nil];
[UIView setAnimationRepeatAutoreverses:true];

// [mapView moveToLatLong:targetLocation]; //no animation but
moves to the right location
mapView.center = [mapView latLongToPixel:targetLocation];// this
at least does some animation but in the wrong direction


[UIView commitAnimations];

I do see some route-me based apps getting it right. So it must be
possible, hopefully just by calling some API I might be overlooking?
Any thoughts?

Vladimir Vyskocil

unread,
Dec 6, 2011, 11:06:11 AM12/6/11
to route-...@googlegroups.com
It's not really possible in general to animate moveToLatLong using beginAnimations/commitAnimations because overlay layers such as RMMarkers, RMPath,... will not be moved synchronously with the map layer. Instead you might broke the moveToLatLong path into small parts and use for example dispatch_sync() or NSOperationQueue to do the small animations in sequence in a background thread (calling the main thread for the GUI operations of course).
Hope it helps.

Vlad.

> --
> You received this message because you are subscribed to the Google Groups "route-me" group.
> To post to this group, send email to route-...@googlegroups.com.
> To unsubscribe from this group, send email to route-me-map...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/route-me-map?hl=en.
>

R Miner

unread,
Dec 8, 2011, 12:28:08 AM12/8/11
to route-me
Great! I was able to get the animation to work the NSOperationQueue
way.

Thanks

On Dec 6, 8:06 am, Vladimir Vyskocil <vladimir.vysko...@gmail.com>
wrote:

Ariel Camus Villalobos

unread,
Dec 14, 2011, 2:46:46 PM12/14/11
to route-...@googlegroups.com
It'd be great if you could share the code. Thanks!

R Miner

unread,
Dec 16, 2011, 1:51:27 AM12/16/11
to route-...@googlegroups.com
code for animation:

1. Somewhere in your constructor/viewdidload, initialize an operation queue with concurrency 1.

    /* Operation Queue init */

    queue = [[NSOperationQueue allocinit];

    [queue setMaxConcurrentOperationCount:1];


2. Add a stepper function that can be called to enqueue pieces of work("frames in animation") to the operation queue. Note the importance of calling a selector on the main thread.

- (void)moveToTargetWithAnimation {

    // if we are on the move, return right away. we want the current move to finish.

    if (is_moving)

        return;

    // remember that we are moving

    is_moving = true;

    

    // get the current location

    from = [mapView pixelToLatLong:mapView.center];

    

    // initialize step and steps (member variables)

    step = 1;

    

    nsteps = 12;

    

    // create 'nsteps' operation objects that will be executed in the queue thread

    for (int i = 0; i < nsteps; i++) {

        NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self

                                                                                selector:@selector(moveToLocationWithOperation)

                                                                                  object:nil];

        

        [queue addOperation:operation];

        [operation release];

    }

}


3. Add an operation handler:

- (void) moveToLocationWithOperation {

    [self performSelectorOnMainThread:@selector(makeMapViewChange) withObject:nil waitUntilDone:NO];

    usleep(20000); // adjust for your tastes

    step++;

    // not moving anymore if we reached the desired number of steps

    if (step == nsteps)

        is_moving = false;

}


4. Add a worker function that does the actual move based on 'step'

- (void) makeMapViewChange {

    // intermediate target

    CLLocationCoordinate2D tmpTarget;

    float p = (1.0 * (nsteps - step)) / nsteps;

    float q = (1.0 * step) / nsteps;

    // this is a linear function. could get fancy with p and q depending on step.

    tmpTarget.latitude = from.latitude * p + target.latitude * q;

    tmpTarget.longitude = from.longitude * p + target.longitude * q;        


    [self.mapView moveToLatLong:tmpTarget];

    if (step == nsteps) {

        [self.mapView moveToLatLong:target];

    }

}




4. Now, when you want to move to a location with animation, you could do it as an IBAction like this:

- (IBAction)moveToLocation:(id)sender {

    target.latitude = <latitude you want to go to>;

    target.longitude = <longitude you want to go to>;

    

    [self moveToTargetWithAnimation];


}


Justin R. Miller

unread,
Dec 16, 2011, 5:41:12 PM12/16/11
to route-me
Do you have any interest in writing this up into a fork and sending a
pull request? I think this would be very useful to the route-me
project in general. I'm going to experiment with it myself.

--
Justin R. Miller
Development Seed, Inc.
jus...@developmentseed.org

Justin R. Miller

unread,
Dec 16, 2011, 6:47:24 PM12/16/11
to route-me
Speaking of this, I'm making this a bit more concise through the use
of addOperationWithBlock instead of separate methods, which reminds me
that route-me doesn't use blocks yet as it is still supporting < iOS
4.0. Do we have any plans for dropping iOS 3.x support anytime soon?

--
Justin R. Miller
Development Seed, Inc.
jus...@developmentseed.org

On Dec 16, 2:41 pm, "Justin R. Miller" <jus...@developmentseed.org>
wrote:

Ariel Camus Villalobos

unread,
Dec 17, 2011, 11:22:40 AM12/17/11
to route-...@googlegroups.com
The map shows a gray overlay until it reaches the movement destination. Do you know why?

Ariel Camus Villalobos

unread,
Dec 19, 2011, 7:01:27 AM12/19/11
to route-...@googlegroups.com
It was my fault, I was using a gray background in stead of a loading tile so it looked gray while moving. The problem I'm seeing now is that the movement speed and the final result is too dependent of the cpu process capacity: in the Simulator the movement is smooth but in a real device it's slower. I'm not sure if is is the right approach.

Justin R. Miller

unread,
Dec 19, 2011, 12:22:05 PM12/19/11
to route-me
I am finding similar poor performance on device.

On Dec 19, 4:01 am, Ariel Camus Villalobos <sargento.ar...@gmail.com>
wrote:

Duan Luu

unread,
Dec 19, 2011, 5:47:09 PM12/19/11
to route-...@googlegroups.com
Hi all,

Yes, you're right. I'm now using route-me for my simple project, but i noticed that the panning speed is quite slow relatively to the native library MapKit. I tried a whole day to understand route-me code so that i can improve panning speed, but i still don't understand how it works. Can someone tell me where in the code need to make a change? Looking at the code, it seems that route-me already uses cached tiles (in both files and RAM), but my app runs slowly even when moving the map back to the place where it has just been. And i think we need to use multithreading to improve panning speed in the case user moves map to new place.  

-- 
Best Regards,
Duan.

--
You received this message because you are subscribed to the Google Groups "route-me" group.

R Miner

unread,
Dec 25, 2011, 6:26:33 PM12/25/11
to route-me
Sorry for the late reply. I was away without Internet connection (one
of those times when you'd need a route-me based offline app). What I
found when I tested on iPhone-4S was acceptable to me(no more flicker
than general route-me panning) but haven't gotten a chance to test on
older models. If there is a solution to reduce the general panning
flicker I think the moveToLatLong animation would benefit as well.

Duan Luu

unread,
Dec 26, 2011, 1:27:14 AM12/26/11
to route-...@googlegroups.com
Thank you very much for your reply. :D You've offered a great idea, i'll try it then. BTW, i'm using an iPhone-3GS.

-- 
Best Regards,
Duan.
Reply all
Reply to author
Forward
0 new messages