I have been fiddling around with Google Maps for iOS and iOS maps for sometime now and in the Google maps sample code I found an awesome animation wherein the the map keeps rotating with a centre. The code that they used was this,
animationTimer = [[NSTimer alloc]initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:0] interval:1.f/30.f target:self selector:@selector(moveCamera) userInfo:nil repeats:YES];
NSRunLoop *defaultRunnLoop = [NSRunLoop currentRunLoop];
[defaultRunnLoop addTimer:preventTimer forMode:NSDefaultRunLoopMode];
- (void)moveCamera{
GMSCameraPosition *camera = self.mapView.camera;
float zoom = fmaxf(camera.zoom - 0.1f, 17.5f);
currentZoomLevel = zoom;
GMSCameraPosition *newCamera =
[[GMSCameraPosition alloc] initWithTarget:rotateAnimationCenter
zoom:zoom
bearing:camera.bearing + 10
viewingAngle:camera.viewingAngle + 10];
[self.mapView animateToCameraPosition:newCamera];
}
What I want to do now is achieve the same in iOS maps by using MKMapCamera, here is the code that I tried so far.
- (void)moveCamera{
MKMapCamera *newCamera = [MKMapCamera cameraLookingAtCenterCoordinate:loc fromEyeCoordinate:[self coordinateFromCoord:loc atDistanceKm:0.001 atBearingDegrees:camera.heading + 2] eyeAltitude:zoom];
[self.mapView setCamera:newCamera animated:YES];
}
- (CLLocationCoordinate2D)coordinateFromCoord:(CLLocationCoordinate2D)fromCoord
atDistanceKm:(double)distanceKm
atBearingDegrees:(double)bearingDegrees
{
double distanceRadians = distanceKm / 6371.0;
//6,371 = Earth's radius in km
double bearingRadians = [self radiansFromDegrees:bearingDegrees];
double fromLatRadians = [self radiansFromDegrees:fromCoord.latitude];
double fromLonRadians = [self radiansFromDegrees:fromCoord.longitude];
double toLatRadians = asin(sin(fromLatRadians) * cos(distanceRadians)
+ cos(fromLatRadians) * sin(distanceRadians) * cos(bearingRadians) );
double toLonRadians = fromLonRadians + atan2(sin(bearingRadians)
* sin(distanceRadians) * cos(fromLatRadians), cos(distanceRadians)
- sin(fromLatRadians) * sin(toLatRadians));
// adjust toLonRadians to be in the range -180 to +180...
toLonRadians = fmod((toLonRadians + 3*M_PI), (2*M_PI)) - M_PI;
CLLocationCoordinate2D result;
result.latitude = [self degreesFromRadians:toLatRadians];
result.longitude = [self degreesFromRadians:toLonRadians];
return result;
}
- (double)radiansFromDegrees:(double)degrees
{
return degrees * (M_PI/180.0);
}
- (double)degreesFromRadians:(double)radians
{
return radians * (180.0/M_PI);
}
MKMapview is not behaving the way I want it to, I changed various properties of the camera but no use. Any help is appreciated.