ルートの方向に従って、地図上で自転車を回転させたい。そのための式を入れましたが、画像がパス上に正しく表示される場合と、パス上にない場合があります。同じことを達成するための別の式を提供するか、これを修正してください:
@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];
}
}