0

我想将自行车/汽车朝哪个方向移动,用户通过指南针方法移动。因为,我使用了 didUpdateHeading 方法,但我仍然无法将自行车图像朝用户的方向移动。

<pre>
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *AnnotationViewID = @"annotationViewID";
    annotationView = (MKAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
    if (annotationView == nil)
    {
        annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
    }    
    annotationView.image = [UIImage imageNamed:@"Bike.png"];
    return annotationView;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    currentLocation = newLocation;
    previousLocation = oldLocation;
    if(currentLocation != nil)
    {
        if (myAnnotation)
        {
            [self.myMapView removeAnnotation:myAnnotation];
        }
        CLLocationCoordinate2D location = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
        myAnnotation = [[MyAnnotation alloc] initWithCoordinates:location title:@"Current Location" subTitle:nil];
        [self.myMapView addAnnotation:myAnnotation];
    }
}
- (void)locationManager:(CLLocationManager*)manager didUpdateHeading:(CLHeading*)newHeading {

    if (newHeading.headingAccuracy > 0) {
        float heading = -1.0f * M_PI * newHeading.magneticHeading / 180.0f;
        annotationView.transform = CGAffineTransformMakeRotation(heading);
    }
}
</pre>
4

1 回答 1

1
- (void)viewDidLoad
{
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;

    [locationManager startUpdatingLocation];

    locationManager.headingFilter = kCLHeadingFilterNone;
    [locationManager startUpdatingHeading];

    [self.view bringSubviewToFront:_compass_image];


    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}


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

    double rotation = newHeading.magneticHeading * 3.14159 / 180;

    [_compass_image setTransform:CGAffineTransformMakeRotation(-rotation)];

}
于 2013-09-05T05:28:04.680 回答