我正在将 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/