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

在iPhone上将经度/纬度转换为x/y,iphone,ios,math,coordinates,latitude-longitude,Iphone,Ios,Math,Coordinates,Latitude Longitude,我正在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)

我正在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];
这给了我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);
但这给了我一些非常奇怪的值: x=140363776.241755,y为91045888.536491

你知道我该怎么做才能让它工作吗


非常感谢

要实现这一目标,您需要了解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;
如果x或y为负数或大于图像大小,则该点不在地图上

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

编辑:

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

double pointLong = pointX / xScale + minLong;
double pointLat = pointY / yScale + minLat;

其中,pointX和pointY表示屏幕点中图像上的一个点。0,0是图像的左上角。

用什么公式来计算x和y?用一个有效的公式:D我刚刚在互联网上搜索了一下,找到了上面的公式。基本上我有我的iPhone屏幕,它显示一个国家的图像,我想通过经纬度坐标在这张图像上找到城市。上面的公式是mecator投影。我想我有类似的东西。这是我的项目的截图:。小黑点应该指向左中间的城市美因茨。不要去不莱梅附近的中上层首先谢谢!但我尝试了这个代码,得到了x:-115.473389 y:-230.305954。但是49.993615点,8.242493点不应该在地图上消失。那么这些值怎么可能是负值,我怎么才能计算出正确的值?!对不起,我的xScale和yScale部分向后。请参阅我对这两行的更新。是否也可以将其从x/y像素转换回经度和纬度?因为我想在我的图像地图上得到两个坐标之间的距离。好的,非常感谢。我在很多城市进行了测试,结果发现每次x值都略有不同。你错过了什么吗?或者仅仅是因为我的最大值和最小长度不够精确?@davidelen如果最小值和最大值不够精确,或者如果图像不是真正的墨卡托投影,那么你的x和y值将关闭。