如何快速将图像旋转到特定的纬度和经度?

Posted

技术标签:

【中文标题】如何快速将图像旋转到特定的纬度和经度?【英文标题】:how to rotate image to a particular latitude and longitude in swift? 【发布时间】:2016-08-03 07:12:13 【问题描述】:

我正在尝试将图像从地球的任何地方旋转到特定方向,例如指向埃菲尔铁塔的箭头标志。我编写了一个用作指南针的程序。它表示东西南北方向。如何更新我的代码以找到埃菲尔铁塔(或任何其他固定地点)的方向。这是代码。

class ViewController: UIViewController,CLLocationManagerDelegate 

  var location: CLLocation!
  var imgArrow: UIImageView!
  var locationManager: CLLocationManager!

  override func viewDidLoad() 
      super.viewDidLoad()

      imgArrow = UIImageView(image: UIImage(named: "arrow.png"))
      self.view.addSubview(imgArrow)

      if(!CLLocationManager.locationServicesEnabled())
         return
      
      locationManager = CLLocationManager()
      locationManager.delegate = self

     if(CLLocationManager.headingAvailable())
        locationManager.startUpdatingHeading()
    
  

 override func viewDidAppear(animated: Bool) 
     imgArrow.center = CGPointMake(CGRectGetMidX(self.view.bounds),    CGRectGetMidY(self.view.bounds))
  
 func locationManagerShouldDisplayHeadingCalibration(manager: CLLocationManager) -> Bool 
    return true
 
 func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) 
    let heading:CGFloat = CGFloat(-M_PI * (newHeading.magneticHeading + 90) / 180.0)


    imgArrow.transform = CGAffineTransformMakeRotation(heading)
    print(heading)
 

【问题讨论】:

Check this question 【参考方案1】:
#define degreesToRadians(x) (M_PI * x / 180.0)
#define radiandsToDegrees(x) (x * 180.0 / M_PI)
- (float)getHeadingForDirectionFromCoordinate:(CLLocationCoordinate2D)fromLoc toCoordinate:(CLLocationCoordinate2D)toLoc
    float fLat = degreesToRadians(fromLoc.latitude);
    float fLng = degreesToRadians(fromLoc.longitude);
    float tLat = degreesToRadians(toLoc.latitude);
    float tLng = degreesToRadians(toLoc.longitude);

    float degree = atan2(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng));

    float deg  = radiandsToDegrees(degree);
    NSLog(@"%f",deg);

    return degreesToRadians(deg);

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
    MKAnnotationView *annotationView = [[MKAnnotationView alloc]init];
        annotationView.image = [UIImage imageNamed:@"ainted_sportscar___top_view_by_balagehun1991.png"];
        annotationView.bounds = CGRectMake(0, 0, 10, 22.5);
        annotationView.transform = CGAffineTransformRotate(mapView.transform, return angle of function);
    return annotatio.View;

也许对你有帮助

【讨论】:

以上是关于如何快速将图像旋转到特定的纬度和经度?的主要内容,如果未能解决你的问题,请参考以下文章

如果纬度/经度上有多个标记,我如何旋转地图标记

在 Three.js 中将经度和纬度转换为 xyz(为了旋转相机)的问题

如何从旋转矩阵中获取用户视点?

Android:通过拖动图像的特定部分来旋转图像

Python旋转图像到特定的角度(Rotate Image to a specific degree)

如何检测将显微镜图像旋转对齐到模板的良好特征