問題描述
在 iPhone 上使用自定義地圖時,我陷入了某種地理位置的困境.問題是:我有一個 UIScrollView
和一個 UIImageView
,其中包含用于展覽會的自定義地圖圖像,以及所有展位位置等.我最大的問題是我打算使用地理定位來定位該地圖內(nèi)的人.當我嘗試獲取給定緯度和經(jīng)度的像素位置時,問題就開始了.我有幾個地面控制點作為參考,甚至設法計算了我的參考點和當前位置之間的角度.問題是我無法將像素距離與使用地理位置坐標計算的距離相關聯(lián).
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.
我試圖將它們關聯(lián)如下:
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 投影的角度,但我找不到正確相乘的比例.我猜是因為緯度和經(jīng)度以度數(shù)給出并且是非線性的,但是我有一個很小的點,我可以將它近似為線性點.
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.
更新:
關注本網(wǎng)站:http://www.movable-type.co.uk/scripts/latlong.html,我可以使用 Haversine 公式計算距離,但作為下面的方向,我發(fā)現(xiàn)我以錯誤的方式計算角度.我將不得不找到另一種計算角度的方法.
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.
推薦答案
我會通過假設平面地球近似并使用矢量代數(shù)將 lat,lon 空間中的角度和距離與 x,y 像素空間相關聯(lián)來做到這一點.
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.
例如:
我假設您知道左下角和右下角的緯度、經(jīng)度.還假設您的公平地圖不在兩極附近并且面積相當小.
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 像素.在下面的偽代碼中調(diào)用了 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
從你想要的人的緯度,經(jīng)度位置計算向量 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.
通過等角投影計算緯度、經(jīng)度空間中的距離:
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
現(xiàn)在將此距離縮放到您的地圖比例
Now scale this distance to your map scale
此時,您已經(jīng)有了像素空間中該點的極坐標
At this point, you have polar coordinates of the point in pixel space
您現(xiàn)在進行極坐標到矩形的轉換;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
作為一個健全的檢查,您可以計算從左上角到左下角創(chuàng)建的緯度、經(jīng)度向量的角度,從右下角到左下角點綴.如果角度不接近 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 中的緯度和經(jīng)度計算 X 和 Y的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!