我必须将注释视图图像添加为汽车。我可以根据我从服务器获得的位置移动该车。但是当我申请汽车轮换代码时,我遇到了问题。每次,汽车转向新的位置,它会移动并再次移动到原来的位置(汽车不会面向前方)。然后它再次转动,移动并移动到原始位置。它应该直线前进。我希望汽车应该面向前方,它应该只在实际转弯时转弯。我的代码是:
oldLocation.latitude = [[_lat objectAtIndex:i] doubleValue];
oldLocation.longitude = [[_longitude objectAtIndex:i] doubleValue];
newLocation.latitude = [[latNew objectAtIndex:i] doubleValue];
newLocation.longitude = [[longNew objectAtIndex:i] doubleValue];
float getAngle = [self angleFromCoordinate:oldLocation toCoordinate:newLocation];
[UIView animateWithDuration:2 animations:^{
MKAnnotationView *annotationView = (MKAnnotationView *[self.mapView viewForAnnotation:myAnnotation[i]];
annotationView.transform = CGAffineTransformMakeRotation(getAngle);
myAnnotation[i].coordinate = newLocation;
}];
}
- (float)angleFromCoordinate:(CLLocationCoordinate2D)first
toCoordinate:(CLLocationCoordinate2D)second {
float deltaLongitude = second.longitude - first.longitude;
float deltaLatitude = second.latitude - first.latitude;
float angle = (M_PI * .5f) - atan(deltaLatitude / deltaLongitude);
if (deltaLongitude > 0) return angle;
else if (deltaLongitude < 0) return angle + M_PI;
else if (deltaLatitude < 0) return M_PI;
return 0.0f;
}