地图上的自定义ImageView不会随着iPhone中的MapView一起旋转

时间:2012-12-06 10:30:45

标签: ios uiimageview ios6 mkmapview

您好我正在处理地图应用程序。我正在旋转mapview和imageview,它是annotationView上的子视图。 Mapview旋转是完美的,但是imageview不是根据Mapview旋转,并且始终指向北方向。

我使用下面的代码来旋转imageview:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation

{

static NSString *identifier = @"annView";

MKPinAnnotationView *annView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

annView=nil;
[annView setSelected:YES animated:NO];
if (annView == nil) 
{
    annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
}

MyAnnotation *myAnnotation=(MyAnnotation *)annotation;
NSString *details;
if ([myAnnotation isKindOfClass:[MKUserLocation class]])
{
    //return nil;
}
else
{
    details=[myAnnotation annType];

}
NSLog(@"Details is  %@",details);
annView.canShowCallout = YES;

if([details isEqualToString:@"Current"])
{
    [[annView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    annView.animatesDrop=NO;
    annView.canShowCallout=YES;//pin3.png// Parking_Psymbol//dus2
    annView.image=[UIImage imageNamed:@""];
    UIImage * locaterImage;
    locaterImage = [UIImage imageNamed:@"Locatermap.png"];

    locaterImageView = [[UIImageView alloc] initWithImage:locaterImage];
    locaterImageView.frame = CGRectMake(-28, -4, 70, 70);

    //--------For animating imageview ---------------------

    // Setup the animation
    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationDuration:2.0];
    [UIView setAnimationCurve:2.0];
    //[UIView setAnimationRepeatCount:10];
    [UIView setAnimationBeginsFromCurrentState:YES];
           [UIView setAnimationDelegate:self];
    [UIView commitAnimations];
    //--------For animating imageview ---------------------
    [annView addSubview:locaterImageView];
}

用于旋转MapView:

-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading

{

// Convert Degree to Radian and move the needle
float oldRad =  -manager.heading.trueHeading * M_PI / 180.0f;
float newRad =  -newHeading.trueHeading * M_PI / 180.0f;

CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
theAnimation.fromValue = [NSNumber numberWithFloat:oldRad];
theAnimation.toValue=[NSNumber numberWithFloat:newRad];
theAnimation.duration = 0.5f;
[mapView.layer addAnimation:theAnimation forKey:@"transform.rotation"];
self.mapView.transform = CGAffineTransformMakeRotation(newRad);
}
我用谷歌搜索,但我找不到我要求的答案。请提出任何解决此问题的想法。

提前致谢。

1 个答案:

答案 0 :(得分:0)

这只是旋转地图以便与现实世界相匹配吗?即北方地图上真的指向北方吗?如果是这样,您需要做的是将MKMapView的{​​{1}}设置为userTrackingMode