使用Compass方向旋转MapView

not*_*oop 6 iphone cocoa-touch iphone-3gs

嵌入式MKMapView旋转是否可以始终面向iPhone面向的方向?基本上我想模仿我自己的应用程序上的地图应用程序旋转功能.

我看到iPhone SDK没有公开这个功能.但是,我想知道是否可以使用旋转整个视图CGAffineTransformMakeRotate.它会影响点击和缩放吗?有没有更好的办法?

thi*_*sai 12

要旋转mapView而不是注释,可以使用以下代码来补偿地图旋转.

- (void)locationManager:(CLLocationManager *)manager
       didUpdateHeading:(CLHeading *)newHeading
{
  double rotation = newHeading.magneticHeading * 3.14159 / 180;
  CGPoint anchorPoint = CGPointMake(0, -23); // The anchor point for your pin

  [mapView setTransform:CGAffineTransformMakeRotation(-rotation)];

  [[mapView annotations] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    MKAnnotationView * view = [mapView viewForAnnotation:obj];

    [view setTransform:CGAffineTransformMakeRotation(rotation)];
    [view setCenterOffset:CGPointApplyAffineTransform(anchorPoint, CGAffineTransformMakeRotation(rotation))];

  }];

}
Run Code Online (Sandbox Code Playgroud)

另一个解决方案是使用iOS 5中添加到MKMapView的新方法.

请查看:http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html

- (void)setUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated;

  • 如何在iOS6中引入此功能?我们可以将地图向北旋转吗?我试图定位地图,但它失败了. (2认同)

Dou*_*ays 8

我可以确认它工作正常.这是我正在使用的代码:

[mapView setTransform:CGAffineTransformMakeRotation(-1 * currentHeading.magneticHeading * 3.14159 / 180)];
Run Code Online (Sandbox Code Playgroud)

mapView 是我的MKMapView实例