[英]How to map OSMSharp view point to Geo coordinate?
I am using OSMSharp for an iOS project. 我正在将OSMSharp用于iOS项目。
I'll like to be able to convert a given point in the view/map to GeoCoordinates (based on the current MapCenter, view size, zoom, etc). 我希望能够将视图/地图中的给定点转换为GeoCoordinates(基于当前的MapCenter,视图大小,缩放等)。
I thought mapView.Map.Project.ToGeoCoordinates would do that, but the result is not correct. 我以为mapView.Map.Project.ToGeoCoordinates可以做到,但是结果不正确。 The inverse function (mapView.Map.Project.ToPixel) returns values in a coordinate system that doesn't correspond to the actual view (it returns value up to 10000 or so, and the size of my view is 1024x768).
逆函数(mapView.Map.Project.ToPixel)在与实际视图不对应的坐标系中返回值(它返回的值最大为10000左右,并且我的视图的大小为1024x768)。
So, how do I convert a given pixel coordinate in a OsmSharp.iOS.UI.MapView to its corresponding GeoCoordinate?. 那么, 如何将OsmSharp.iOS.UI.MapView中的给定像素坐标转换为其对应的GeoCoordinate?
Right now there's no method to get it. 目前没有任何方法可以得到它。 But it's easy to get
但是很容易得到
Add below method to MapView class in OsmSharp.iOS.UI project 在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);
}
If you want the Point from Geocoordinate, add below method to the same class 如果要从地理坐标指向点,请将以下方法添加到同一类
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));
}
And below method to Matrix2D class in OsmSharp.UI.Renderer project 以及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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.