2012-06-05 73 views
6

我知道如何找到真正的航向/磁航向我的電話是在目前的位置,但有可能找到一個遠程位置的磁偏差/偏角?CLLocation - 尋找磁偏角/偏差在另一個位置

我想要做的是能在一個地方在地圖上放置圖釘,並發現無論是真方位,並從該點磁場變化的影響。

謝謝!

+0

我的一個想法是把它寫到CLLocation的位置屬性,但最終是隻讀的,所以這是窗外。 –

回答

4

你有解決它?否則,您可以計算遠程位置的方位角。首先是磁性的北方航向,然後是真正的北方航向。最後你減去兩個得到磁偏差。

這裏是如何計算的方位角爲遠程位置:

-(float)azimuthFromLocations:(CLLocationCoordinate2D)first toCoordinate:(CLLocationCoordinate2D)second{ 
float longitudeDifference = second.longitude - first.longitude; 
float latitudeDifference = second.latitude - first.latitude; 
float possibleAzimuth = (M_PI * .5f) - atan(latitudeDifference/longitudeDifference); 

if (longitudeDifference > 0) 
    return possibleAzimuth; 
else if (longitudeDifference < 0) 
    return possibleAzimuth + M_PI; 
else if (latitudeDifference < 0) 
    return M_PI; 

return 0.0f; 
} 
+0

謝謝,該解決方案最終變得稍微複雜一些。我不得不使用世界磁模型來計算給定座標的偏差。 –

+0

*變化。偏差在傳感器中。 –

+0

該解決方案僅在赤道附近(近似)爲真。當你接近極點時,方位角必須基於距離,而不是緯度/經度座標。 – MikeMayer67

3

的代碼來計算這必須在一個框架已經存在的地方,因爲它使用CLHeading當位置服務。

如果任何人都可以找到代碼或具有世界磁場模型發佈它,將不勝感激目標C。

更新:我發現了一個偉大的開源iOS的包裝!感謝Crookneck! https://github.com/stephent/ObjectiveWMM

安裝簡單,作爲一個git子模塊

run this command: 
$git submodule add https://github.com/stephent/ObjectiveWMM.git ObjectiveWMM 

Add these files to your Xcode project: 
CCMagneticDeclination.h 
CCMagneticDeclination.m 
CCMagneticModel.h 
CCMagneticModel.m 
NSDate+DecimalYear.h 
NSDate+DecimalYear.m 
WMM/EGM9615.h 
WMM/GeomagnetismHeader.h 
WMM/GeomagnetismLibrary.c 
WMM/WMM.COF