如何将OSMSharp视点映射到Geo坐标?

时间:2014-10-14 22:25:33

标签: ios openstreetmap

我正在将OSMSharp用于iOS项目。

我希望能够将视图/地图中的给定点转换为GeoCoordinates(基于当前的MapCenter,视图大小,缩放等)。

我认为mapView.Map.Project.ToGeoCoordinates会这样做,但结果不正确。反函数(mapView.Map.Project.ToPixel)返回与实际视图不对应的坐标系中的值(它返回的值最多为10000左右,我的视图大小为1024x768)。

那么,如何将OsmSharp.iOS.UI.MapView中的给定像素坐标转换为相应的GeoCoordinate?

1 个答案:

答案 0 :(得分:1)

现在没有办法获得它。但它很容易获得

将以下方法添加到OsmSharp.iOS.UI项目中的MapView类

    public GeoCoordinate PointToCoordinate(int PointX, int PointY)
    {
        View2D view = _cacheRenderer.Create(this.Width, this.Height, this.Map,
               (float)this.Map.Projection.ToZoomFactor(this.MapZoom), this.MapCenter, false, true);

        // get scene coordinates.
        double x, y;
        var fromMatrix = view.CreateFromViewPort(this.Width, this.Height);
        fromMatrix.Apply(PointX, PointY, out x, out y);

        return this.Map.Projection.ToGeoCoordinates(x, y);
    }

如果您想要Geocoordinate中的Point,请将以下方法添加到同一个类

    public Point CoordinateToPoint(GeoCoordinate gc)
    {
        var gcPx = this.Map.Projection.ToPixel(gc);
        View2D view = _cacheRenderer.Create(this.Width, this.Height, this.Map,
               (float)this.Map.Projection.ToZoomFactor(this.MapZoom), this.MapCenter, false, true);

        // get scene coordinates.
        double x, y;
        var fromMatrix = view.CreateFromViewPort(this.Width, this.Height);
        fromMatrix.Remove(gcPx[0], gcPx[1], out x, out y);

        return new Point((int)System.Math.Round(x, 0), (int)System.Math.Round(y, 0));
    }

以下是OsmSharp.UI.Renderer项目中Matrix2D类的方法

    public void Remove(double x, double y, out double xNew, out double yNew)
    {
        xNew = (x * this.E11 - this.E02 * this.E11 + this.E01 * this.E12 - this.E01 * y) / (this.E00 * this.E11 - this.E01 * this.E10);
        yNew = (y - this.E12 - this.E10 * xNew) / this.E11;
    }