This project has moved and is read-only. For the latest updates, please go here.

Rounding Errors in MapPolyLine

Sep 19, 2013 at 6:52 PM
Hey guys,

I've noticed a weird issue when you're really zoomed in. If you bind a MapPolyLine to a collection of Locations, you get weird rounding issues when you zoom in or pan around. I'll throw in some screenshots to give you an idea of what I'm talking about.

The green dots are at the same positions as where the line should be. They lines pop and jerk too, which is hard to capture in a screenshot. I can upload a sample project if necessary.


Sep 19, 2013 at 8:23 PM
I have already observed this kind of behaviour. It is a result of very large scaling factors in the map's ViewportTransform and does not only emerge with MapPolylines, but with any kind of map overlays when zoomed in to high zoom levels. There is not really much that could be done to avoid this behaviour, as it is inherent in geometry transforms in WPF. A workaround might be to write a different MapPolyline class that does not use WPF transforms at all, but recalculates all the viewport coordinates each time the viewport changes.
Sep 19, 2013 at 10:28 PM
Heh, that's really annoying! I just did some research and it seems like other people have had that issue when trying to use their own unit system. Here's the one that really stuck with me.

Seems like we're just pushing WPFs API to the limits. Telling it to do things that it wasn't designed for. I'm guessing there is no easy way to reform the math to do everything scaled locally? I'm willing to investigate this and put some leg work into it, if you have any suggestions on where to start looking and brainstorming.
Sep 20, 2013 at 12:26 PM
No, there is obviously no easy way to "reform the math". For the purpose of writing your own polyline control that reacts on the map's ViewportChanged event you may start looking into the LocationToViewportPoint method in class MapBase. It just does
return ViewportTransform.Transform(mapTransform.Transform(location));
where mapTransform is a MercatorTransform instance that transforms locations to logical points, and ViewportTransform is the transform mentioned above which transforms logical points to viewport points (and apparently has the rounding problem). You may now delve into how ViewportTransform is calculated in class TileContainer and try to resemble the maths in a way that avoids the rounding issue.
Oct 16, 2013 at 10:26 PM
Hey dude!

We were able to figure out a workaround for this. We cloned MapPolyline and replaced the StreamGeometry with a pen. It's probably a bit more expensive, but it doesn't jitter! We call it MapPolypen. You want a copy of it? And if so, how can I get you a pull request or whatever?
Oct 16, 2013 at 11:05 PM
Just post it here and I'll have a look.
Oct 17, 2013 at 1:01 AM
Here's a Google Drive link to all of the zipped files. I made some small edits to MapPath for this so I included those. If you can think of a clever way to get around the need to edit MapPath, then you can probably remove any changes.
Oct 17, 2013 at 10:34 AM
Edited Oct 17, 2013 at 3:16 PM
Your workaround is a typical "it solves my special problem" solution.
  1. Although your MapPolypen still derives from MapPath, it completely overrides its Geometry handling.
  2. It ignores the Fill property.
  3. It ignores the IsClosed property, hence does not draw closed polygons.
As you have already copied a lot of code from MapOverlay, it's hard to understand why you didn't come up with the obvious solution, which would be just deriving a Polyline class from MapOverlay. It could look as simple as this:
public class MapPolylineOverlay : MapOverlay
    public static readonly DependencyProperty LocationsProperty =
            "Locations", typeof(IEnumerable<Location>), typeof(MapPolylineOverlay),
            new FrameworkPropertyMetadata(
                null, FrameworkPropertyMetadataOptions.AffectsRender));

    public IEnumerable<Location> Locations
        get { return (IEnumerable<Location>)GetValue(LocationsProperty); }
        set { SetValue(LocationsProperty, value); }

    protected override void OnViewportChanged()

    protected override void OnRender(DrawingContext drawingContext)
        if (ParentMap != null && Locations != null)
            Location loc1 = null;
            foreach (Location loc2 in Locations)
                if (loc1 != null)
                loc1 = loc2;