Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 1    Views: 100

AussieRyan
Aspiring developer
Profile
Posts: 148
Reg: Mar 21, 2012
Margate, QLD Au...
1,480
08/19/16 04:13 PM (7 years ago)

mapview does not zoom on the first pass into the view controller

Can anybody help me please? I'm a novice to xcode (I can google but don't always understand) and am baffled by what I think is a small problem. For some reason the first time my map is opened from my initial view controller the pin is dropped but the map does not zoom to the correct region even though the latitude and longitude are correct. When I go back to the first view controller and launch it again the zoom works. The code: ======================================================================= // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; [[self mapView] setMapType:MKMapTypeHybrid]; // Suppprt iPhone 5 if ([UIScreen mainScreen].bounds.size.height == 568){ UIImageView *bkv = (UIImageView*)[self.view viewWithTag:1000]; bkv.image = [UIImage imageNamed:@"Bkg-MapIt-568h"]; } } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } #pragma mark - #pragma mark Setter -(void)setLocation:(CLLocationCoordinate2D)newCoordinate { ELog(@"Setting location to: %f %f",newCoordinate.latitude,newCoordinate.longitude); MKCoordinateRegion region; region.center = newCoordinate; region.span.latitudeDelta = 0.001; region.span.longitudeDelta = 0.001; [[self mapView] setRegion:region animated:YES]; coordinate = newCoordinate; [[self mapView] removeAnnotation:self]; [[self mapView] addAnnotation:self]; } #pragma mark - #pragma mark IBAction -(void)dismissMap { //[self dismissModalViewControllerAnimated:YES]; [[self navigationController] popViewControllerAnimated:YES]; } etc..... ====================================================================== I have seen on stackflow I should try doing that in viewDidAppear instead. If you're prepared for it to zoom to that location every time the view appears. viewDidLoad is too early in the mapview cycle. Not knowing code, how can I change the code if this is the correct solution?
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
08/25/16 07:13 AM (7 years ago)
This kinda looks like custom code rather than a rework of an existing plugin... We can't see all of the code, so we can't know what settings you applied to your map, what triggers the 'setLocation' and a few other pertinent methods... When I built the SmugMsgLoc, it was a great learning experience, but what I learned the most was David put a lot of built in methods that do a lot of work for us. It did me a lot of good to study his mapping methods as I put mine together. In fact, I stole quite a bit of code from the 'bt_screen_map' plugin; why reinvent the wheel. That being said, one useful thing I did was 'pre-set' the map to center on my regional location. CLLocationCoordinate2D tmpLocation; tmpLocation.latitude = 13.5; // change your values, naturally. tmpLocation.longitude = 144.8; [self.mapView setCenterCoordinate:tmpLocation animated:FALSE]; and placed it just after the map was created. This let the initial map view load pretty close to where the user already was, and didn't make it seem so odd. And take a look at the bt_screen_map 'centerMap' function. There's probably a few things that would solve your issue in that method, depending on how the other parts of your class are configured... Cheers! -- Smug
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.