The issue
I've got an application that tracks vehicles on a map. However I cannot get the little buggers to rotate in the direction of their motion. Basically, all of them are going sideways! Ugh!
The code
Image vehicleImage = new Image
{
//Set image size and source
};
RenderTransform rotation= new RotateTransform{Angle = X};
vehicleImage.RenderTransfrom = rotation;
_mainMap.Children.Add(vehicleImage);
MapControl.SetLocation(vehicleImage, _position);
The Image, placed on the map, seems to completely ignore any angle I try to apply.
To understand why the rotation doesn't take effect, let's first take a look at the image below, which is taken from the Live Visual Tree in Visual Studio -
I use a
Rectangle
but in your case, you will see yourImage
control there instead. When you insert it into theMapControl.Children
collection, it will be wrapped with a special element calledMapOverlayPresenter
(as shown in the picture).This
MapOverlayPresenter
is an internal element withinMapControl
and surprisingly, there's no official documentation on the Internet on what exactly it does. My guess is, as you zoom or rotate the map, this overlay simply responds by zooming or rotating in the opposite direction in order to maintain the original size and rotation of the child element, and this is causing the rotation transform of your innerImage
to somehow get lost.(P.S.
RotationAngle
andRotationAngleInDegrees
from Composition have no effect here either.)Solution
The way to work around this is simple - instead of exposing the rotation transform on the
Image
directly, create aUserControl
calledImageControl
which encapsulates thisImage
and its transform, with dependency properties likeUriPath
andAngle
that are responsible for passing the information down to the innerImage
and itsCompositeTransform
property.The
ImageControl
XAMLThe
ImageControl
code-behindHow to use this
ImageControl
Outcome
Hope this helps!