2012-05-23 104 views
5

我正在研究一些iPhone應用程序,並且我想根據用戶位置使用加速計來創建方位,移動到特定地理位置的圖像。iOS:從沒有地圖的當前位置獲取地理位置的角度

我在這裏閱讀了很多答案,但沒有得到解決方案。

我有當前的位置座標和目標位置。

你有任何想法或示例代碼?謝謝。

+0

見[此](http://stackoverflow.com/questions/6745131/how-can-we-find-the-angle-between-two-locations-defined-by-latitude-or-longitude )和[this](http://www.movable-type.co.uk/scripts/latlong.html)以供參考 –

回答

9

在最前的.h文件

float GeoAngle; 

在外景經理的委託方法定義此

#define RadiansToDegrees(radians)(radians * 180.0/M_PI) 
#define DegreesToRadians(degrees)(degrees * M_PI/180.0) 

定義變量: -

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    GeoAngle = [self setLatLonForDistanceAndAngle:newLocation]; 
} 

,功能將作如下: -

-(float)setLatLonForDistanceAndAngle:(CLLocation *)userlocation 
{ 
    float lat1 = DegreesToRadians(userlocation.coordinate.latitude); 
    float lon1 = DegreesToRadians(userlocation.coordinate.longitude); 

    float lat2 = DegreesToRadians(locLat); 
    float lon2 = DegreesToRadians(locLon); 

    float dLon = lon2 - lon1; 

    float y = sin(dLon) * cos(lat2); 
    float x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon); 
    float radiansBearing = atan2(y, x); 
    if(radiansBearing < 0.0) 
    { 
     radiansBearing += 2*M_PI; 
    } 

    return radiansBearing; 
} 

您將在GeoAngle變量中不斷更新角度。 將箭頭旋轉到目標位置拍攝箭頭圖像並將其分配給arrowImageView的IBOutlet,並在標題更新方法上旋轉此項。

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading 
{ 
    float direction = -newHeading.trueHeading; 

    arrowImageView.transform=CGAffineTransformMakeRotation((direction* M_PI/180)+ GeoAngle); 
} 
+0

謝謝你我瞭解角度,但是如何使用加速度計正確旋轉我的圖像以顯示此方向?我在這個方法中使用了角度: - (void)加速度計:(UIAccelerometer *)加速度計didAccelerate:(UIAcceleration *)加速度;但即使我旋轉iPhone也不會更新。 – hafedh

+0

如果你想將箭頭指向目標位置,然後再次檢查答案,我編輯答案。 – Dhawal

+0

它工作!非常感謝你。 – hafedh

相關問題