我想根据iOS设备方向旋转我的地图视图,我实现如下
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
self.userDirection = newHeading.trueHeading;
self.mapView.camera.heading = newHeading.trueHeading;
}
然后我遇到一个问题,当mapView的相机旋转时,mapView无法缩放。
我该如何解决这个问题?
答案 0 :(得分:0)
我意识到MKMapCamera
是iOS7中的一个新类,可能它不稳定。
最后,我以另一种方式实现了mapView旋转:
首先,我放大mapView
#pragma mark - Map View Setup
- (void)setupMapView
{
// setup size
double edgeLength = sqrt(pow(screenSize.size.width, 2) + pow(screenSize.size.height, 2));
self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, edgeLength, edgeLength)];
self.mapView.center = screenCenter;
self.mapView.delegate = self;
}
下一步,在CLLocationManager Delegate
中实现地图旋转#define DEGREES_TO_RADIANS(degrees) ((degrees / 180.0) * (M_PI))
...
...
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
double radians = DEGREES_TO_RADIANS(newHeading.trueHeading);
[self.mapView setTransform:CGAffineTransformMakeRotation(-radians)];
}
完成!
答案 1 :(得分:0)
初始化MKMapView时应尝试使用方法- (void)setUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated
,例如:
self.mapView.delegate = self
self.mapView.setUserTrackingMode(MKUserTrackingModeFollowWithHeading, animated: true)
我的地图根据此代码的标题进行了旋转。
答案 2 :(得分:0)
在MKMapView的委托中,只需设置一个标志来检查用户是否正在尝试缩放:
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
isTouching = YES;
}
并在更改后将其关闭:
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
isTouching = NO;
}
并仅在未触及的情况下更改标题:
- (void)changeMapHeading:(CLLocationDirection)heading
{
if (!isTouching)
{
targetMapView.camera.heading = heading;
}
}
顺便说一句,您可能还想减少从LocationManager开始的标题更新频率:
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
NSDate *now = [NSDate date];
NSTimeInterval diff = [now timeIntervalSinceDate:lastHeadingUpdateTime];
if (diff > 0.1)
{
lastHeadingUpdateTime = now;
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationTrackingHeadingUpdated object:newHeading];
}
}