在iPhone上将经度/纬度转换为x / y [英] Convert longitude/latitude to x/y on iPhone

查看:147
本文介绍了在iPhone上将经度/纬度转换为x / y的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am showing an image in an UIImageView and i'd like to convert coordinates to x/y values so i can show cities on this image. This is what i tried based on my research:

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。

which gives me 167 as x and y=104 but this example should have the values x=73 and y=294.

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

mapView is my UIImageView, just for clarification.

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

So my second try was to use the 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。

But this gives me some really strange values: x = 140363776.241755 and y is 91045888.536491.

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

So do you have an idea what i have to do to get this working ?

非常感谢!

推荐答案

要完成这项工作,你需要要了解4条数据:

To make this work you need to know 4 pieces of data:


  1. 图像左上角的纬度和经度。

  2. 图像右下角的纬度和经度。

  3. 图像的宽度和高度(以磅为单位)。

  4. 纬度和经度数据点。

  1. Latitude and longitude of the top left corner of the image.
  2. Latitude and longitude of the bottom right corner of the image.
  3. Width and height of the image (in points).
  4. Latitude and longitude of the data point.

使用该信息,您可以执行以下操作:

With that info you can do the following:

// 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 为负或大于图像的大小,然后该点不在地图上。

If x or y are negative or greater than the image's size, then the point is off of the map.

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

This simple solution assumes the map image uses the basic cylindrical projection (Mercator) where all lines of latitude and longitude are straight lines.

编辑:

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

To convert an image point back to a coordinate, just reverse the calculation:

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

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

where pointX and pointY represent a point on the image in screen points. (0, 0) is the top left corner of the image.

这篇关于在iPhone上将经度/纬度转换为x / y的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆