本文介绍了在iPhone中帮助从经度和纬度计算X和Y.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用iPhone上的自定义地图卡住了某种地理位置限制。问题是:我有一个 UIScrollView 和一个 UIImageView 展台位置等。
我最大的问题是我打算使用地理位置来定位该地图内的人。当我尝试获取给定经度和纬度的像素位置时,问题就开始了。我有几个地面控制点作为参考,甚至设法计算出我参考点与当前位置之间的角度。问题是,我无法将像素距离与用地理位置坐标计算出的距离相关联。



我试图将它们关联如下:



计算我地图中已知点的像素距离。
使用两个参考点的矢量距离计算来计算距离。
等于上面两个找到实际参考距离。

但我没有成功......

使用余弦定律,我可以找到能够给出我对X和Y的投影的角度,但我无法找到正确乘法的比例。我想是因为经度和纬度是以度数表示的,而且是非线性的,但我有一个很小的光点,我可以将它作为线性光点。



我无法在MKMapKit中使用叠加图像,因为我必须水平使用地图,并且在Google地图中,同一位置向左旋转几度。 / p>

更新:

关注此网站:,我可以使用Haversine公式计算距离,但作为取向波纹管,我发现我以错误的方式计算角度。我将不得不寻找另一种方法来计算角度。

解决方案

我会通过假设平面地球近似并使用矢量代数将纬度,lon空间中的角度和距离与x,y像素空间关联。

例如:


p>我假设你知道左下角和右下角的lat,lon。另外假设你的公平地图不在极点附近,并且面积相当小。



选取一个坐标系,用左下角的已知纬度,lon为0,0像素。在以下伪代码中称为lat1,lon2

计算从右下角lat2,lon2到左下角lat1,lon1的矢量1

使用简单投影xl = lon,yl = lat,则向量1 =(lon2-lon1)i +(lat2-lat1)j bb
$ b

计算向量2从lat,lon你想要的位置(latp,lonp)放在地图上的左下角lat1,lon1

使用矢量点积得到矢量1和2之间的角度。

通过equirectangular投影计算经纬度,lon空间的距离:

<$ p
p1 =(lonp - lon1)cos(0.5 *(latp + lat1))(将经纬度转换为弧度)
p2 =(latp - lat1)
距离= R * sqrt(p1 * p1 + p2 * p2)
使用R = 6371000,您的距离将以米为单位

现在将此距离缩放到您的地图比例



此时,您在像素空间中具有极坐标点



你现在做一个极性到矩形的转换锡安; x = r cos(角度),y = r sin(角度)

r是缩放距离(即像素空间距离),angle是矢量1和2上面

作为一个完整性检查,您可以计算从左上角到左下角创建的lat,lon矢量的角度,点右下角到左下角。如果角度不接近90度,则可能会出现太多失真。


I'm stuck into some kind of geolocation limbo using a custom map with iPhone. The problem is: I have an UIScrollView and an UIImageView with a image of a custom map for a fair, with all stand locations and so on.My biggest problem is that I intend to use geolocation in order to locate the person inside that map. The problem begins when I try to get the pixel location of a given latitude and longitude. I have several ground control points as reference and even managed to calculate the an angle between my reference spot and the current location. The problem is that I can't correlate the distance in pixels with a distance calculated with the geolocation coordinates.

I am trying to relate them as follows:

Calculating the distance in pixels of a known point in my map.Calculating the distance using vector distance calculation for two reference points.Equaling the two above to find the actual distance from the reference.

But I am having no success at all...

Using the law of cosines, I can find the angle that will give my projections of X and Y but I can't find a scale to multiply correctly. I guess is because latitude and longitude are given in degrees and are non-linear, but I have such a small spot that I can aproximate it as linear spot.

I can't use an overlay image in MKMapKit because I have to use the map horizontally and in google maps, the same place is rotate several degrees to the left.

UPDATE:

Following this site: http://www.movable-type.co.uk/scripts/latlong.html, i could calculate the distance using the Haversine formula, but as orientation bellow, i found out that I was calculating the angle in a wrong way. I will have to find another way to calculate the angle.

解决方案

I would do this by assuming flat earth approximation and use vector algebra to relate angles and distances in the lat,lon space to the x,y pixel space.

For example:

I am assuming you know the lat,lon for the bottom left and bottom right corners. Also assuming your fair map isn't near the poles and is fairly small in area.

Pick a coordinate system say bottom left corner with known lat,lon at 0,0 pixels. Called lat1,lon2 in following pseudo code

Compute vector 1 from the bottom right lat2,lon2 to the bottom left lat1,lon1

Using simple projection xl=lon, yl=lat, then vector 1 = (lon2 - lon1)i + (lat2-lat1)j

Compute vector 2 from the lat,lon position of person you want (latp,lonp) to put on the map to the bottom left point lat1,lon1

Use vector dot product to get the angle between vector 1 and 2.

Compute the distance in lat,lon space via equirectangular projection:

p1 = (lonp - lon1) cos ( 0.5*(latp+lat1) ) (convert lat/lon to radians)
p2 = (latp - lat1)
distance = R * sqrt( p1*p1 + p2*p2)
use R = 6371000 and your distance will be in meters

Now scale this distance to your map scale

At this point, you have polar coordinates of the point in pixel space

you now do a polar to rectangular conversion; x = r cos(angle), y = r sin(angle)

r is the scaled distance (i.e. distance in pixel space) and angle is the angle between vector 1 and 2 above

As a sanity check, you could compute the angle of the lat,lon vectors created from the top left to bottom left dotted with the bottom right to bottom left. If the angle isn't close to 90 degrees, there may be too much distortion for your purposes.

这篇关于在iPhone中帮助从经度和纬度计算X和Y.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 16:35