Friday, 24 February 2012

Zoom into a MKMapView in IOS SDK - Programmatically


MKMapvIew Screenshot
MKMapvIew
This article will tell you how can we can zoom into a MKMapView programmatically in an IOS app. IOS is known for its beautiful animations and  zoom in to a MKMapView animation is one of them.

In this article, we will show that how can be animate map view so that it appears to user that we are zooming map view to his location. Displaying user location on map is an essential feature to apps which provide information about things that user uses in his day to day life style.We in our apps can display user location along with various store outlets that cater with user day to day needs on map.


To create map in our iPhone application we use MKMapView Class.




In .h file of your viewController, create an instance of MKMapView

MKMapView* _mapView;

Also set delegates for _mapView.

Now open your .m file and navigate to viewDidload, here  call your method showMap which alloocate and initiate your _mapView instance of MKMapView.

-(void)viewDidLoad
{
   [self showMap];
}

Now define your showMap method as shown in below code snippet

-(void) showMap {
_mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 410)];
[self.view addSubview:_mapView];
[_mapView setDelegate:self];
[self performSelector:@selector(zoommap) withObject:nil afterDelay:.5];
}

In above code we are creating an instance on our MKMapView class and giving it frames. Then adding it to our view as a subview.Then we  are setting MKMapview delegates.

Define zoommap method as shown in below code snippet, in this we  are giving region to or mapView instance a value of 0, so that it show map at minimum zoom level.Then we are giving latitude and logitude where we want to centre our map.For zoom in we are changing region span values at the end.

-(void)zoommap
{
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
NSString *ltf = [[NSString alloc]initWithFormat:@"%@",[arrLatitudeNew objectAtIndex:0]];
NSString *lntf =[[NSString alloc]initWithFormat:@"%@",[arrLongitudeNew objectAtIndex:0]];
double ltdf = [ltf doubleValue]; double lntdf = [lntf doubleValue];
region.center.latitude = ltdf ;
region.center.longitude = lntdf;
[ltf release];
[lntf release];
// for zooming the map
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[_mapView setRegion:region animated:YES];
[_mapView setDelegate:self];
}


1 comment:
Write comments
  1. nice article, thanks for sharing

    ReplyDelete