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

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

CGFloat height = mapView.frame.size.height; CGFloat width = mapView.frame.size.width; int x = (int) ((width/360.0) * (180 + 8.242493)); //Main lon int y = (int) ((height/180.0) * (90 - 49.993615)); //Mainz lat NSLog(@"x:%iy:%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件数据:

  1. 图像左上angular的经度和纬度。
  2. 图像右下angular的经度和纬度。
  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; 

如果xy是负值或大于图像的大小,则该点离开地图。

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

编辑:

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

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

其中pointXpointY表示屏幕点中图像上的一个点。 (0,0)是图像的左上angular。