ios - 如何将OSMSharp视点映射到地理坐标?

标签 ios openstreetmap

我正在将 OSMSharp 用于 iOS 项目。

我希望能够将 View / map 中的给定点转换为地理坐标(基于当前的 map 中心、 View 大小、缩放等)。

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

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

最佳答案

目前没有办法获取它。不过很容易得到

将以下方法添加到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);
    }

如果您想要来自地理坐标的点,请将以下方法添加到同一类中

    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;
    }

关于ios - 如何将OSMSharp视点映射到地理坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26371443/

相关文章:

objective-c - 如何以编程方式检测 iOS 中是否开启背光?

api - 如何使用 OpenStreetMap 进行地理编码和路由?

Javascript地理定位找不到位置信息

c++ - github 上的 map 库。 C++/Qt/OpenStreetMap

ios - 无法分配类型为“[String : AnyObject]

iphone - 推送通知不调用该方法

ios 应用程序崩溃,没有日志

ios - 无法使用导航 Controller 锁定 View Controller 上的设备方向

java - 如果我事先不知道它的对象模型,我该如何解析一个 json 字符串?

routes - GraphHopper 车辆=汽车可以工作,但步行或自行车不起作用