MKMapView – zoom Out To view the whole World
Sometimes, in maps, you might need to display entire-world-view. Example, in case of no location found, zoom out to the world view. Following code-snip would help you to achieve the same.
Sometimes, in maps, you might need to display entire-world-view. Example, in case of no location found, zoom out to the world view. Following code-snip would help you to achieve the same.
1. Copy following file in to projects. Place.h Place.m PlaceMark.h PlaceMark.h 2. Download above files from Here & place it as follows. 3. Create new file (myCity.m) 4. Place following code (myCity.h)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#import <uikit /UIKit.h> #import <mapkit /MapKit.h> #import "Place.h" #import "PlaceMark.h" #import "MyCityDetails.h" @interface myCity : UIViewController<mkmapviewdelegate> { IBOutlet MKMapView* mapView; NSMutableArray *reports; MyCityDetails *nxtDetails; } @property(nonatomic,retain) IBOutlet MKMapView* mapView; @property(nonatomic,retain) NSMutableArray *reports; @end |
5. Place following code (myCity.m)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
-(void)viewWillAppear:(BOOL)animated { NSArray *AS=[NSArray arrayWithArray:[reports objectAtIndex:0]]; for (int i=0; i<[AS count]; i++) { Place* home = [[[Place alloc] init] autorelease]; home.name = [[AS objectAtIndex:i] valueForKey:@"comments"]; home.latitude = [[[AS objectAtIndex:i] valueForKey:@"latitude"]floatValue]; home.longitude = [[[AS objectAtIndex:i] valueForKey:@"longitude"]floatValue]; PlaceMark* from = [[[PlaceMark alloc] initWithPlace:home] autorelease]; [mapView addAnnotation:from]; } [self centerMap]; [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
-(void) centerMap { MKCoordinateRegion region; CLLocationDegrees maxLat = -90; CLLocationDegrees maxLon = -180; CLLocationDegrees minLat = 120; CLLocationDegrees minLon = 150; NSArray *temp=[NSArray arrayWithArray:[NSArray arrayWithArray:[reports objectAtIndex:0]]]; for (int i=0; i<[temp count];i++) { Place* home = [[[Place alloc] init] autorelease]; home.latitude = [[[temp objectAtIndex:i] valueForKey:@"latitude"]floatValue]; home.longitude =[[[temp objectAtIndex:i] valueForKey:@"longitude"]floatValue]; PlaceMark* from = [[[PlaceMark alloc] initWithPlace:home] autorelease]; CLLocation* currentLocation = (CLLocation*)from ; if(currentLocation.coordinate.latitude > maxLat) maxLat = currentLocation.coordinate.latitude; if(currentLocation.coordinate.latitude < minLat) minLat = currentLocation.coordinate.latitude; if(currentLocation.coordinate.longitude > maxLon) maxLon = currentLocation.coordinate.longitude; if(currentLocation.coordinate.longitude < minLon) minLon = currentLocation.coordinate.longitude; region.center.latitude = (maxLat + minLat) / 2; region.center.longitude = (maxLon + minLon) / 2; region.span.latitudeDelta = maxLat - minLat; region.span.longitudeDelta = maxLon - minLon; } [mapView setRegion:region animated:YES]; } |