Note that there are some explanatory texts on larger screens.

plurals
  1. POMKMapView reset back to world view: Rebaked
    primarykey
    data
    text
    <p>I have been having ongoing memory leak problems with MKMapView (reported to Apple, but no response), and so as a work-around I tried to just allocate one MKMapView and re-use that map instead of reallocating a new one. The problem I'm running into with this is I have yet to be able to reset the MKMapView to the view it starts with. I assume because I'm working from the USA, I initially get a view like this: </p> <p><a href="http://www.cprince.com/stackoverflow/map1.png" rel="nofollow noreferrer">map of USA http://www.cprince.com/stackoverflow/map1.png</a></p> <p>With this code:</p> <pre><code>static MKMapView *map = nil; - (void) showUserLocation { [map setShowsUserLocation:YES]; CLLocationCoordinate2D coord; coord.latitude = 39.0; // changed from original coords coord.longitude = -105.0; MKCoordinateRegion r = MKCoordinateRegionMakeWithDistance(coord, 1000, 1000); [map setRegion:r animated:YES]; } - (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if (! map) { CGRect r = CGRectMake(20, 50, 196, 75); map = [[MKMapView alloc] initWithFrame:r]; defaultRegion = MKCoordinateRegionMake(map.centerCoordinate, MKCoordinateSpanMake(180, 360)); } else { [map setRegion:defaultRegion animated:YES]; } [self.view addSubview:map]; } - (IBAction)annotateMap:(id)sender { [self showUserLocation]; } </code></pre> <p>when I click on the button that calls annotateMap, I get the following map (no surprises yet):</p> <p><a href="http://www.cprince.com/stackoverflow/map2.png" rel="nofollow noreferrer">map of my home http://www.cprince.com/stackoverflow/map2.png</a></p> <p>then, when I navigate away (using a navigation controller) from this view and then re-enter the view (viewWillAppear is again called, but this time it re-uses the MKMapView previously allocated), I get the following map view:</p> <p><a href="http://www.cprince.com/stackoverflow/map3.png" rel="nofollow noreferrer">map of ocean http://www.cprince.com/stackoverflow/map3.png</a></p> <p>and of course I should be getting the original default region because of this part of the viewWillAppear:</p> <pre><code> defaultRegion = MKCoordinateRegionMake(map.centerCoordinate, MKCoordinateSpanMake(180, 360)); } else { [map setRegion:defaultRegion animated:YES]; } </code></pre> <p>It doesn't matter if do the setRegion after the addSubview or before. That is, the change below doesn't correct the probelem:</p> <pre><code>- (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; BOOL firstTime = NO; if (! map) { firstTime = YES; CGRect r = CGRectMake(20, 50, 196, 75); map = [[MKMapView alloc] initWithFrame:r]; defaultRegion = MKCoordinateRegionMake(map.centerCoordinate, MKCoordinateSpanMake(180, 360)); } [self.view addSubview:map]; if (! firstTime) { [map setRegion:defaultRegion animated:YES]; } } </code></pre> <p>and using MKMapRectWorld doesn't help either:</p> <pre><code>defaultRegion = MKCoordinateRegionForMapRect(MKMapRectWorld); </code></pre> <p>The problem is also not due to some kind of race condition with MKMapView. If I alter the handler for the button:</p> <pre><code>- (IBAction)annotateMap:(id)sender { defaultRegion = MKCoordinateRegionMake(map.centerCoordinate, MKCoordinateSpanMake(180, 360)); [self showUserLocation]; } </code></pre> <p>and wait until the map is settled on the USA view before I click the button, the problem is the same.</p> <p>I am using iOS 6.1.2 and XCode 4.6.</p> <p>(The full XCode project is available at <a href="http://www.cprince.com/stackoverflow/Map.zip" rel="nofollow noreferrer">http://www.cprince.com/stackoverflow/Map.zip</a>).</p> <p>Thoughts?</p> <p>FIRST CHANGE</p> <p>I made the defaultRegion a static instead of an instance variable and this doesn't change the situation markedly. (I reported the lat/long for the default region below). Here's the result in the map:</p> <p><a href="http://www.cprince.com/stackoverflow/map4.png" rel="nofollow noreferrer">map of ocean http://www.cprince.com/stackoverflow/map4.png</a></p> <p>For clarity, the code as it stands now is:</p> <pre><code>static MKMapView *map = nil; static MKCoordinateRegion defaultRegion; - (void) showUserLocation { [map setShowsUserLocation:YES]; CLLocationCoordinate2D coord; coord.latitude = 39.0; // changed from original coord.longitude = -105.0; MKCoordinateRegion r = MKCoordinateRegionMakeWithDistance(coord, 1000, 1000); [map setRegion:r animated:YES]; } - (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; BOOL firstTime = NO; if (! map) { firstTime = YES; CGRect r = CGRectMake(20, 50, 196, 75); map = [[MKMapView alloc] initWithFrame:r]; defaultRegion = MKCoordinateRegionMake(map.centerCoordinate, MKCoordinateSpanMake(180, 360)); NSLog(@"defaultRegion: center: lat: %f, long: %f; span: %f dlat: dlong: %f", defaultRegion.center.latitude, defaultRegion.center.longitude, defaultRegion.span.latitudeDelta, defaultRegion.span.longitudeDelta); //defaultRegion = MKCoordinateRegionForMapRect(MKMapRectWorld); } [self.view addSubview:map]; if (! firstTime) { NSLog(@"defaultRegion: center: lat: %f, long: %f; span: %f dlat: dlong: %f", defaultRegion.center.latitude, defaultRegion.center.longitude, defaultRegion.span.latitudeDelta, defaultRegion.span.longitudeDelta); [map setRegion:defaultRegion animated:YES]; } } - (IBAction)annotateMap:(id)sender { //defaultRegion = MKCoordinateRegionMake(map.centerCoordinate, MKCoordinateSpanMake(180, 360)); [self showUserLocation]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload