Question

I've seen it on other applications exmaple ios 6 starbucks, when my mapview is opened, I want it to show the region as the whole of UK/British isles, then I want it to zoom in to my specified location region points I have.

Updated Code:

- (void)viewDidLoad
{

[super viewDidLoad];
// Do any additional setup after loading the view.
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 54.5;
region.center.longitude = -3.5;
region.span.longitudeDelta = 10.0f;
region.span.latitudeDelta = 10.0f;
[mapView setRegion:region animated:NO];

[self performSelector:@selector(zoomInToMyLocation)
           withObject:nil
           afterDelay:2]; //will zoom in after 1.5 seconds
}

-(void)zoomInToMyLocation
{
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 51.502729 ;
region.center.longitude = -0.071948;
region.span.longitudeDelta = 0.19f;
region.span.latitudeDelta = 0.19f;
[mapView setRegion:region animated:YES];

[mapView setDelegate:self];

[self performSelector:@selector(selectAnnotation)
           withObject:nil
           afterDelay:0.5]; //will zoom in after 0.5 seconds

}

-(void)selectAnnotation
{

DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = @"Design Museum";
ann.subtitle = @"Camberwell, London";
ann.coordinate = region.center;
[mapView addAnnotation:ann];

}

Dont know if its correct, because the error is this line

ann.coordinate = region.center;
Was it helpful?

Solution

If you want to start with showing one region and then zoom in, you have to issue two or more setRegion calls because setRegion by itself doesn't let you control the starting region or the speed of the animation.

In viewDidLoad, set the initial region's span so the entire UK is visible (try deltas of 10.0 instead of 0.15). You could also set animated to NO for the initial region.

Then before the end of viewDidLoad, schedule the zoom-in to be executed a few seconds later:

- (void)viewDidLoad
{
    ...

    [self performSelector:@selector(zoomInToMyLocation) 
               withObject:nil 
               afterDelay:5]; //will zoom in after 5 seconds
}

The zoomInToMyLocation method might look like this:

-(void)zoomInToMyLocation
{
    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
    region.center.latitude = 51.502729 ;
    region.center.longitude = -0.071948;
    region.span.longitudeDelta = 0.15f;
    region.span.latitudeDelta = 0.15f;
    [mapView setRegion:region animated:YES];
}


One thing you might have to take care of when using performSelector is to cancel a pending call if the view is closed or deallocated before the call is scheduled to run. For example, if the user closes the view two seconds after loading it. Three seconds later, the scheduled method may still get called but will crash since the view is gone. To avoid this, cancel any pending performs in viewWillDisappear: or wherever appropriate:

[NSObject cancelPreviousPerformRequestsWithTarget:self];

OTHER TIPS

MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = Your Latitude ;
region.center.longitude = Your Longitude;
region.span.longitudeDelta =  0.01f;
region.span.latitudeDelta =  0.01f;
[map setRegion:region animated:YES];
[map addAnnotation:ann];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top