使用指南针方向旋转MapView [英] Rotate MapView using Compass orientation

查看:150
本文介绍了使用指南针方向旋转MapView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

解决方案

要旋转mapView但不是注释,你可以使用下面的代码来补偿地图旋转。

   - (void)locationManager:(CLLocationManager *)manager 
didUpdateHeading:(CLHeading *)newHeading
{
double rotation = newHeading.magneticHeading * 3.14159 / 180;
CGPoint anchorPoint = CGPointMake(0,-23); //你的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))];

}];

}






解决方案使用的是在iOS 5中添加到MKMapView的新方法。



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



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

Is it possible to have an embedded MKMapView rotate to always face the direction the iPhone is facing? Basically I want to mimic the Map app rotation feature on my own app.

I see that the iPhone SDK does not expose the functionality. However, I wonder if it would work to rotate the entire view using CGAffineTransformMakeRotate. Would it affect tapping and zooming? Is there a better way?

解决方案

To rotate the mapView but not the annotations you could use the following code to compensate for the maps rotation.

- (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))];

  }];

}


Another sollution is using a new method that has been added in iOS 5 to MKMapView.

Take a look at: http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html

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

这篇关于使用指南针方向旋转MapView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆