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

标签 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) * (90 - 49.993615)); // Mainz lat


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

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

[self.view addSubview:pinView];

x 为 167,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. 图片左上角的纬度和经度。
  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;

如果 xy 为负数或大于图像大小,则该点不在 map 上。

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

编辑:

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

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

其中 pointXpointY 表示图像上的一个点(以屏幕点为单位)。 (0, 0) 是图像的左上角。

关于iphone - 在 iPhone 上将经度/纬度转换为 x/y,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14185584/

相关文章:

iPhone : Sharepoint GetListItemChangesSinceToken method returns item recursively

javascript - 在 iPhone Safari 上使用事件委托(delegate)处理 HTML SELECT 选项到 javascript

iphone - 强制 UIImagePickerController 以横向模式拍摄视频

ios - UISlider 的 Avplayer 问题

iOS颜色在UIImage中透明

math - 模运算的时间复杂度

iphone - #import <libxml/HTMLparser.h> 要导入什么库

ios - 您如何管理 iOS 项目中的 View 标签?

math - 如何均匀地随机化球体表面上的点?

c - 定点码分理解