Map region on Universal application

Hello.


I have an application with two storyboards (one for iPad and one for iPhone). I use the following to set the map area:


  -(void) viewWillAppear:(BOOL)animated
    {
        ...
      
        [self.mapView removeAnnotations:self.mapView.annotations];
        self.mapLocations = [[NSMutableArray alloc]init];
        self.showMapRect = MKMapRectNull;
        [self addAnnotation];
        [self.mapView setVisibleMapRect:self.showMapRect edgePadding:UIEdgeInsetsMake(30, 30, 30, 30) animated:YES];
    }
   
   
    -(void) addAnnotation
    {
        CLLocationCoordinate2D mapLocation;
       
        IGAMapAnnotation *mapAnnotation;
       
        // If a set of coordinates is empty, it will have an empty object @"".
        // This object must be removed from the array, otherwise it will be an error.
        // In order not to change the original array, we create a copy
       
        NSMutableArray *coordinatesTempArray = [[NSMutableArray alloc]initWithArray:self.coordinatesArray copyItems:YES];
        [coordinatesTempArray removeObject:@""];
       
        // Calculate how many points are included in the final flightplan
   
        NSInteger numberOfPoints = [coordinatesTempArray count];
       
        // Annotations will be added only of the flight plan includes at least one point
        if (numberOfPoints > 0) {
           
            // Place all annotations so that are are visible on the map using MKMapRect
            for (NSInteger i=0; i < ([coordinatesTempArray count]); i++)
            {   
                mapAnnotation = [[IGAMapAnnotation alloc]init];
               
                // Taking a point in the array and getting its coordinates
                self.mapCoordinates = [coordinatesTempArray objectAtIndex:i];
               
                // Getting a point in the array and getting its latitude and longitude
                self.mapLatitude = [[self.mapCoordinates objectAtIndex:0]doubleValue];
                self.mapLongitude = [[self.mapCoordinates objectAtIndex:1]doubleValue];
               
                // Assigning the point coordinates to the coordinates to be displayed on the map
                mapLocation.latitude = self.mapLatitude;
                mapLocation.longitude = self.mapLongitude;
               
                // Adding coordinates to the map annotation
                mapAnnotation.coordinate = mapLocation;
               
                // Adding the annotation to the array that will be added to the map
                [self.mapLocations addObject:mapAnnotation];
               
                // Adding Annotation coordinates to MKMapRect so that they all be visible in the view
                MKMapPoint annotationMapPoint = MKMapPointForCoordinate(mapLocation);
                MKMapRect annotationMapRect = MKMapRectMake(annotationMapPoint.x, annotationMapPoint.y, 0, 0);
               
                self.showMapRect = MKMapRectUnion(self.showMapRect, annotationMapRect);
               
            }
        }
       
        // Adding annotations to the map
        [self.mapView addAnnotations:self.mapLocations];
       
    }


It looks good on iPad (I wish I could upload images -- do not see this option). When the map opens, it has exactly the required span, all points on the map are visible. However, when I open the map on iPhone, the map is zoomed in, I see the central part, and the points are outside of the map boundaries.


My iPad application is landscape only, and iPhone -- portrait only.


Is there a way to have a map rect that would fit both?


Thanks a lot!

Answered by junkpile in 35814022

Two of those are redundant; I would remove the first and third if the intent is to resize it to fit the margins. In fact the third and last ones will conflict if the bottom margin changes to something nonzero - you would see errors in your console log.


Anyway. That doesn't answer the question 🙂 What is the frame at runtime? Is it the size you would expect?

What is the frame size of the MKMapView on the iPhone? (Is it being resized properly to fit the screen?)

junkpile.


I use auto layout.


Map View.Center X = Superview.Center X

Map View.Top to Top Layout Guide.Bottom = 0

Map View.Bottom to Superview.Bottom = 0

Map View.Trailing to Superview.Trailing Margin = 0

Map View.Leading to Superview.Leading Margin = 0

Map View.Bottom to Superview.Bottom Margin = 0


Cheers.

Accepted Answer

Two of those are redundant; I would remove the first and third if the intent is to resize it to fit the margins. In fact the third and last ones will conflict if the bottom margin changes to something nonzero - you would see errors in your console log.


Anyway. That doesn't answer the question 🙂 What is the frame at runtime? Is it the size you would expect?

Thanks. Removed the first one. The third one was a typo -- I typed the same one twice.


This is what NSLog shows me for the mapview frame:


SIZE OF MAP: WIDTH - 568.000000 : HEIGHT - 536.000000


There is something wacky here. The map size above shows me a landscape size, whereas the iPhone orientation should be portrait. And it is Portrait in Utilities -> Simulated Metrics -> Orientation.


When I put the actual width and height in constraints, this seems to solve the problem. But when I put vertical and horizontal spaces to superview, this problem occurs again. And I would not want to have a specific height and width.


So, how do I fix that?


Thank you very much!


EDIT:


Seems that I have fixed it using the following:


if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        CGRect frameSize = [self.view frame];
        float frameWidth = frameSize.size.width;
        float frameHeight = frameSize.size.height;
        self.mapView.frame = CGRectMake(0, 0, frameWidth, frameHeight);
    }


junkpile. Thank you so much for directing me to the solution!


Cheers.

You're welcome... but that's not a "solution" if you're using Auto Layout. Next time a layout pass occurs (try showing the double height status bar, for example) you will be back to where auto layout thinks your frames should be. You should never set the frame or center properties of a view directly if using auto layout.


It sounds more like there is a problem with missing constraints on the superview, or the whole VC / storyboard is set to not use auto layout, or you're adding a view in code but not setting translatesAutoresizingMaskIntoConstraints to NO or something. You shouldn't have to set the frame anywhere.

Map region on Universal application
 
 
Q