Search code examples
c#rotationuwprotatetransformuwp-maps

How to rotate a polygon on a map with the long/lat coordinates?


I want to rotate a polygon (for example this arrow) by replacing the hardcoded offsets values with a function where can pass in the nord pointing offset and the amount of degrees I want the arrow to rotate at.

I tried using a Rotation matrix but that did not work well.
Probably because the distance of 1 Lat. != 1 Long.

What I'm trying to do with this:
The arrow must represent a vehicle and rotate on the direction the vehicle is heading.

double centerLatitude = pos.Coordinate.Point.Position.Latitude;
double centerLongitude = pos.Coordinate.Point.Position.Longitude;

MapPolygon mapPolygon = new MapPolygon();
mapPolygon.Path = new Geopath(new List<BasicGeoposition>() {
    new BasicGeoposition() {Longitude=centerLongitude, Latitude=centerLatitude},//top of the arrow
    new BasicGeoposition() {Longitude=centerLongitude-0.001, Latitude=centerLatitude-0.0010},
    new BasicGeoposition() {Longitude=centerLongitude-0.001, Latitude=centerLatitude-0.0030},
    new BasicGeoposition() {Longitude=centerLongitude+0.001, Latitude=centerLatitude-0.0030},
    new BasicGeoposition() {Longitude=centerLongitude+0.001, Latitude=centerLatitude-0.0010},              
});
mapPolygon.ZIndex = 1;
mapPolygon.FillColor = Colors.Orange;
mapPolygon.StrokeColor = Colors.Blue;
mapPolygon.StrokeThickness = 2;
mapPolygon.StrokeDashed = false;
MainMap.MapElements.Add(mapPolygon);

enter image description here


Solution

  • MapPolygons are bound to coordinates on the map. Rotation transforms in UWP won't apply to MapPolygons. If you want to rotate a MapPolygon, you have to calculate the rotated coordinates. The Bing Maps V8 map control (web) provides a function for doing this. Here are the two different ways it does this:

    Spatially accurate (may not look the same due to map projection)

    Pixel Accurate (will look the same as you rotate it, but won't be spatially accurate)

    • Calculate the center of the polygon and use this as the anchor point for rotating the polygon around. You could use any other part of the polygon if you desire. Convert this to a global pixel coordinate at zoom level 19: https://msdn.microsoft.com/en-us/library/bb259689.aspx (LatLongToPixelXY)
    • Loop through each location in the MapPolygon and calculate it's global pixel coordinate at zoom level 19.
    • Calculate the rotated pixel coordinates: http://homepages.inf.ed.ac.uk/rbf/HIPR2/rotate.htm
    • Convert the pixel coordinates back into spatial locations at zoom level 19(PixelXyToLatLong)
    • The pass the newly calculated locations into the MapPolygon.