Change the coordinate system of a Canvas in WPF

后端 未结 8 1769
一整个雨季
一整个雨季 2021-02-01 06:59

I\'m writing a mapping app that uses a Canvas for positioning elements. For each element I have to programatically convert element\'s Lat/Long to the canvas\' coordinate, then

相关标签:
8条回答
  • 2021-02-01 07:31

    I'm pretty sure you can't do that exactly, but it would be pretty trivial to have a method which translated from lat/long to Canvas coordinates.

    Point ToCanvas(double lat, double lon) {
      double x = ((lon * myCanvas.ActualWidth) / 360.0) - 180.0;
      double y = ((lat * myCanvas.ActualHeight) / 180.0) - 90.0;
      return new Point(x,y);
    }
    

    (Or something along those lines)

    0 讨论(0)
  • 2021-02-01 07:32

    Another possible solution:

    Embed a custom canvas (the draw-to canvas) in another canvas (the background canvas) and set the draw-to canvas so that it is transparent and does not clip to bounds. Transform the draw-to canvas with a matrix that makes y flip (M22 = -1) and translates/scales the canvas inside the parent canvas to view the extend of the world you're looking at.

    In effect, if you draw at -115, 42 in the draw-to canvas, the item you are drawing is "off" the canvas, but shows up anyway because the canvas is not clipping to bounds. You then transform the draw-to canvas so that the point shows up in the right spot on the background canvas.

    This is something I'll be trying myself soon. Hope it helps.

    0 讨论(0)
提交回复
热议问题