0

我想根据路线的方向在地图上旋转自行车。我已经为此设置了公式,但图像有时会正确显示在路径上,有时不会显示在路径上。请提供另一个公式来实现相同的目标,或在此进行更正:

@interface MAViewController : UIViewController <MKMapViewDelegate,CLLocationManagerDelegate>
{
    CLLocation *currentLocation;
    CLLocation *previousLocation;
}

@end

- (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"];
    float fLat = degreesToRadians(previousLocation.coordinate.latitude);
    float fLng = degreesToRadians(previousLocation.coordinate.longitude);
    float tLat = degreesToRadians(currentLocation.coordinate.latitude);
    float tLng = degreesToRadians(currentLocation.coordinate.longitude);

    float degree = radiandsToDegrees(atan2(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng)));

    annotationView.transform = CGAffineTransformMakeRotation(degree);

    return annotationView;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if (newLocation.horizontalAccuracy < 0)
        return;
    if  (!newLocation)
        return;

    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];
    }
}
4

0 回答 0