Flex offers a number of different animation techniques. For my Route Player application, I wanted to offer an animation speed option so that when the user clicks on the Start button,
the animation starts: the Flex horizontal slider moves to the right, the time-line on the elevation chart moves accordingly, and the Google map gets updated. To see what I mean, click on the thumbnail, and when the Flex application starts, click on the Start button. To make this all work, first you need to define a Flex Sequence control, and include an AnimateProperty that specifies the duration of the animation.
<mx:Sequence id="myMove" target="{slider}">
<mx:AnimateProperty property="value"
fromValue="{timeMin}"
toValue="{timeMax}"
duration="{selectedItem.data}" />
</mx:Sequence>
Next, you’ll need to extend the HSlider control and override the set value method. See the control Note19HSlider. This extension of HSlider, includes a callback function that is set to the method sliderMove, which gets called each time the value of the slider has been updated, this in turn updated the chart and the Google Map. The code for all of this is here.
Each time the value of the Slider changes, the following function gets called:
private function sliderMove(val:String):String {
point0["x"] = val;
point0["y"] = 0;
point1["x"] = val;
point1["y"] = elevationMax;
// timeLineData is a Bindable object; the time line
// on the elevation chart is dependent on this object.
timeLineData.setItemAt(point0, 0);
timeLineData.setItemAt(point1, 1);
// Use binary search to find the closest elevation point to the
// value of the slider
var index : int = binarySearch(Number(val), 0, elevationData.length-1);
var lat : Number = elevationData[index].lat;
var lon : Number = elevationData[index].lon;
// Update the Google map
var u:URLRequest =
new URLRequest("javascript:centerMap('" + lat + "','" + lon + "')");
navigateToURL(u,"_self");
return "Time: " + String(val);
}
The extension to the Slider is provided as an MXML file (Note19HSlider.mxml):
<mx:Script>
<![CDATA[
public var callback : Function = null;
override public function set value(val:Number):void {
super.value = val;
// callback is initialized to be the sliderMove function.
callback(val);
}
]]>
</mx:Script>
November 24, 2007 at 6:46 am |
Nice, let me try!
Thanks,
Pratheep
May 23, 2008 at 7:16 pm |
Very cool, thanks for sharing. I played with it and the result of moving the slider was erratic; sometimes it would just move to the top left corner of the screen.
I think that your approach of using the current elevation from the chart and trying to find it in the route using binary search could return a “false positive”, by finding another point in the route that has a close-enough elevation.
Wouldn’t it be better to keep track of how you are in the route (distance from starting point) and using that information as the “common thread” between the slider and the map? This might allow you to also control the slider position by moving the map marker along the route.
May 24, 2008 at 12:45 am |
Thank you Nascif. I’ve not looked at the code for a while, and your suggestion appears to be bang on. Next time I’m in that code, I’ll try to implement your idea. Meanwhile, if you’ve the time…please let me know
December 3, 2008 at 4:33 am |
Hey SJ,
The project is simply awesome. Great work, great logic in it U must have given pretty good time working on it.
Now I am having a problem
1. when I press start button, there is no animation,
but when I manually move slide it, map animates according to points.and also when I place cursor on chart, map panTO points specified in gpx file.
can u please help me out.
Thank you in anticipation
December 3, 2008 at 4:42 am |
Hi Bhumish, thank you for the kind words. not sure why the start button is not working for you. I’m afraid it could be browser dependent. Also this was one of first attempts, I did perform a number of iterations on this and here is the latest. I wonder if you have a better luck. The JavaScript code for this can be viewed right on your browser (and the actionscript code is also on my Google Project site — note19. Good luck.
December 5, 2008 at 10:48 pm |
Any way I can get source code for this. This would be handy for my bike club.
Thanks
December 8, 2008 at 3:45 am |
Hello Frank, you can get the source code from here (take a look at the ActionScript code in map.mxml.