在iPhone上将经度/纬度转换为x/y

deh*_*len 6 iphone math latitude-longitude coordinates ios

我在UIImageView中显示图像,我想将坐标转换为x/y值,以便我可以在此图像上显示城市.这是我根据我的研究尝试的:

CGFloat height = mapView.frame.size.height;
CGFloat width = mapView.frame.size.width;


 int x =  (int) ((width/360.0) * (180 + 8.242493)); // Mainz lon
 int y =  (int) ((height/180.0) * (90 - 49.993615)); // Mainz lat


NSLog(@"x: %i y: %i", x, y);

PinView *pinView = [[PinView alloc]initPinViewWithPoint:x andY:y];

[self.view addSubview:pinView];
Run Code Online (Sandbox Code Playgroud)

这给了我167作为x和y = 104但这个例子应该有x = 73和y = 294的值.

mapView是我的UIImageView,只是为了澄清.

所以我的第二次尝试是使用MKMapKit:

CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(49.993615, 8.242493);
MKMapPoint point = MKMapPointForCoordinate(coord);
NSLog(@"x is %f and y is %f",point.x,point.y);
Run Code Online (Sandbox Code Playgroud)

但这给了我一些非常奇怪的值:x = 140363776.241755,y是91045888.536491.

所以你知道我必须做些什么才能使这个工作吗?

非常感谢!

rma*_*ddy 8

要完成这项工作,您需要知道4个数据:

  1. 图像左上角的纬度和经度.
  2. 图像右下角的纬度和经度.
  3. 图像的宽度和高度(以磅为单位).
  4. 数据点的纬度和经度.

有了这些信息,您可以执行以下操作:

// These should roughly box Germany - use the actual values appropriate to your image
double minLat = 54.8;
double minLong = 5.5;
double maxLat = 47.2;
double maxLong = 15.1;

// Map image size (in points)
CGSize mapSize = mapView.frame.size;

// Determine the map scale (points per degree)
double xScale = mapSize.width / (maxLong - minLong);
double yScale = mapSize.height / (maxLat - minLat);

// Latitude and longitude of city
double spotLat = 49.993615;
double spotLong = 8.242493;

// position of map image for point
CGFloat x = (spotLong - minLong) * xScale;
CGFloat y = (spotLat - minLat) * yScale;
Run Code Online (Sandbox Code Playgroud)

如果x或者y是负数或大于图像的大小,则该点不在地图上.

这个简单的解决方案假设地图图像使用基本圆柱投影(墨卡托),其中所有纬度和经度线都是直线.

编辑:

要将图像点转换回坐标,只需反转计算:

double pointLong = pointX / xScale + minLong;
double pointLat = pointY / yScale + minLat;
Run Code Online (Sandbox Code Playgroud)

其中pointXpointY代表屏幕点上图像上的一个点.(0,0)是图像的左上角.