iOS百度地圖 根據(jù)兩個經(jīng)緯度計算角度

上代碼

1.首先計算斜率方法: fromCoord是上一個經(jīng)緯度 , toCoord是下一個經(jīng)緯度
- (double)getSlopWithFromCoord:(CLLocationCoordinate2D)fromCoord toCoord:(CLLocationCoordinate2D)toCoord {
    double fromLat = fromCoord.latitude;
    double fromlon = fromCoord.longitude;
    double toLat = toCoord.latitude;
    double tolon = toCoord.longitude;

    if (tolon == fromlon) {
        return MAXFLOAT;
    }
    double slope = ((toLat - fromLat) / (tolon - fromlon));
    return slope;

}
2.計算角度方法: fromCoord是上一個經(jīng)緯度 , toCoord是下一個經(jīng)緯度
分四個象限計算,同時判斷x和y軸
- (double)getAngleWithFromCoord:(CLLocationCoordinate2D)fromCoord toCoord:(CLLocationCoordinate2D)toCoord {
    double fromLat = fromCoord.latitude;
    double fromlon = fromCoord.longitude;
    double toLat = toCoord.latitude;
    double tolon = toCoord.longitude;
    
    double slope = [self getSlopWithFromCoord:fromCoord toCoord:toCoord];
    
    if (slope == MAXFLOAT) {
        if (toLat > fromLat) {
            return 0;
        } else {
            return 180;
        }
    }
    double radio = atan(slope);
    double angle = 180 * (radio / M_PI);
    if (slope > 0) {
        if (tolon < fromlon) {
            angle = -90 - angle;
        } else {
            angle = 90 - angle;
        }
    } else if (slope == 0) {
        if (tolon < fromlon) {
            angle = -90;
        } else {
            angle = 90;
        }
    } else {
        if (toLat < fromLat) {
            angle = 90 - angle;
        } else {
            angle = -90 - angle;
        }
    }
    return  angle;
}
3.如何使用:
- (void)running {
   HomeCarAnnotationView *carAnnotationView = (HomeCarAnnotationView *)[self.mapView viewForAnnotation:self.currentCarAnntation]; 
   double angle = [self getAngleWithFromCoord:self.fromCoor toCoord:self.toCoor];
   carAnnotationView.imageView.transform = CGAffineTransformMakeRotation(angle*M_PI/180); //此處需要計算最終角度
}
4.圖片朝向向上
21F49B2A-0CF9-4865-986F-F19A8248223D.png
5. 如果圖片是反向的:
- (void)running {
   HomeCarAnnotationView *carAnnotationView = (HomeCarAnnotationView *)[self.mapView viewForAnnotation:self.currentCarAnntation]; 
   double angle = [self getAngleWithFromCoord:self.fromCoor toCoord:self.toCoor];
   carAnnotationView.imageView.transform = CGAffineTransformMakeRotation(-(angle*M_PI/180)); //此處需要計算最終角度
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容