在 iPhone 上使用自定义地图时,我陷入了某种地理位置的困境.问题是:我有一个 UIScrollView
和一个 UIImageView
,其中包含用于展览会的自定义地图图像,以及所有展位位置等.我最大的问题是我打算使用地理定位来定位该地图内的人.当我尝试获取给定纬度和经度的像素位置时,问题就开始了.我有几个地面控制点作为参考,甚至设法计算了我的参考点和当前位置之间的角度.问题是我无法将像素距离与使用地理位置坐标计算的距离相关联.
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...
使用余弦定律,我可以找到可以给出 X 和 Y 投影的角度,但我找不到正确相乘的比例.我猜是因为纬度和经度以度数给出并且是非线性的,但是我有一个很小的点,我可以将它近似为线性点.
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.
我不能在 MKMapKit 中使用叠加图像,因为我必须水平使用地图,而在谷歌地图中,同一个地方向左旋转几度.
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.
更新:
关注本网站:http://www.movable-type.co.uk/scripts/latlong.html,我可以使用 Haversine 公式计算距离,但作为下面的方向,我发现我以错误的方式计算角度.我将不得不找到另一种计算角度的方法.
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.
我会通过假设平面地球近似并使用矢量代数将 lat,lon 空间中的角度和距离与 x,y 像素空间相关联来做到这一点.
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.
例如:
我假设您知道左下角和右下角的纬度、经度.还假设您的公平地图不在两极附近并且面积相当小.
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.
选择一个坐标系,比如左下角,已知 lat,lon 为 0,0 像素.在下面的伪代码中调用了 lat1,lon2
Pick a coordinate system say bottom left corner with known lat,lon at 0,0 pixels. Called lat1,lon2 in following pseudo code
计算从右下角lat2,lon2到左下角lat1,lon1的向量1
Compute vector 1 from the bottom right lat2,lon2 to the bottom left lat1,lon1
使用简单的投影xl=lon,yl=lat,然后向量1 = (lon2 - lon1)i + (lat2-lat1)j
Using simple projection xl=lon, yl=lat, then vector 1 = (lon2 - lon1)i + (lat2-lat1)j
从你想要的人的纬度,经度位置计算向量 2 (latp,lonp) 到地图左下角的点 lat1,lon1
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
使用向量点积得到向量1和2之间的角度.
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
您现在进行极坐标到矩形的转换;x = r cos(角度), y = r sin(角度)
you now do a polar to rectangular conversion; x = r cos(angle), y = r sin(angle)
r 是缩放距离(即像素空间中的距离),angle 是上面向量 1 和 2 之间的角度
r is the scaled distance (i.e. distance in pixel space) and angle is the angle between vector 1 and 2 above
作为一个健全的检查,您可以计算从左上角到左下角创建的纬度、经度向量的角度,从右下角到左下角点缀.如果角度不接近 90 度,则可能存在太多失真.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!