Assignment #5 Task #6

Please note, this blog entry is from a previous course. You might want to check out the current one.

When a map view of photos appears on screen, its region should be set to the minimum size that fits all of its annotations (with an aesthetically-pleasing padding around them).

First iterate over all annotations and save the minimum and maximum values for longitude and latitude. Next create a region and set its center and size using this variables. To get the aesthetically-pleasing padding increase the size of the region by 10 %. If there is only on annotation the size would be zero, which can be prevented by randomly set it to a square of one by one degrees.

        FlickrAnnotation *annotation = [self.annotations lastObject]; 
        CLLocationCoordinate2D min = annotation.coordinate;
        CLLocationCoordinate2D max = min;
        for (FlickrAnnotation *annotation in self.annotations) {
            if (annotation.coordinate.latitude < min.latitude)
                min.latitude = annotation.coordinate.latitude;
            else if (annotation.coordinate.latitude > max.latitude)
                max.latitude = annotation.coordinate.latitude;
            if (annotation.coordinate.longitude < min.longitude)
                min.longitude = annotation.coordinate.longitude;
            else if (annotation.coordinate.longitude > max.longitude)
                max.longitude = annotation.coordinate.longitude;            
        }
        MKCoordinateRegion region;
        region.center.longitude = (min.longitude + max.longitude) / 2;
        region.center.latitude = (min.latitude + max.latitude) / 2;
        region.span.latitudeDelta = (max.latitude - min.latitude) * 1.1;
        if (!region.span.latitudeDelta) region.span.latitudeDelta = 1;
        region.span.longitudeDelta = (max.longitude - min.longitude) * 1.1;
        if (!region.span.longitudeDelta) region.span.longitudeDelta = 1;
        [self.mapView setRegion:region];

The complete code for this task is available at github.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

8 thoughts on “Assignment #5 Task #6”

  1. Hi, thank you for the solution.
    I did try my own, which is pretty much like yours, except that I don’t check for the span size of zero (which doesn’t give me any problem).

    The only problem I had was with the 10% aesthetically-pleasing padding.
    It makes no difference in my map if I add the *1.1 or not.
    I did notice that if I add *1.2, still doesn’t make any difference, but if I add *1.3 it doubles the span size (both latitude and longitude).

    It appears that the span can only assume specific values:

    If I try to set: (with 30% padding)
    latitudeDelta: 0.837850 longitudeDelta: 0.442349
    I get after regionThatFits:
    latitudeDelta: 1.588684 longitudeDelta: 1.757812

    If I try to set: (with 20% padding)
    latitudeDelta: 0.773400 span.longitudeDelta:0.442349
    I get after regionThatFits:
    latitudeDelta: 0.794350 longitudeDelta: 0.878906

    Any ideas why? And how to solve it?

      1. Here is the code: https://www.dropbox.com/sh/9sejdswkizriyij/qZzOFqz9KO

        The code is in the commented part of coordinateRegionForMapAnnotations: inMapView:
        method, and in the MapViewController class.

        And would you happen to know why my program crashes when I try to display my map for topPlaces? (if you select a place and then press the map button for photos it works fine)

        Also, would you know why my pins were showing (2 days ago) and now they are not with MKAnnotationView instead of MKPinAnnotationView in mapView:viewForAnnotation: method? By the way, i didn’t touch my code in those 2 days.

        Thank you.

  2. I just found out that the method regionThatFits: (that I was calling manually, even though it is not necessary), changes my region from
    latitudeDelta:103.443000 longitudeDelta:330.827000
    to
    latitudeDelta:177.437555 longitudeDelta:450.000000

    That is probably the reason why the aesthetically-pleasing padding is not working.
    I’m my opinion, the method regionThatFits: should change only one of the Delta’s, either latitude or longitude, to fit the ratio of the screen, but not change both. Also it is changing to default values, in the way that, for example, latitudeDelta can never be 177, it must be 177.437555

    Any ideas of how to solve this?

    1. If you change only one of the deltas and it is the “wrong” one your change will not have an effect, thus it is better to adjust both deltas and let the library do the rest (like calculating the correct delta to prevent stretching the map)?

      1. By adjust you mean the 10% padding right?
        I did that, still, the library changes both deltas, and not just only one.

  3. Hey, first thanks for all the nice work!

    Just a suggestion:
    What do you think about this code?

    Instead of doing all the coordinate calculating business, let MapKit do this for you:

    – (void)updateMapView
    {
    if (self.mapView.annotations)
    [self.mapView removeAnnotations:self.mapView.annotations];
    if (self.annotations) {
    [self.mapView addAnnotations:self.annotations];
    MKMapRect zoomRect = MKMapRectNull;
    for (FlickrAnnotation *annotation in self.annotations) {
    MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
    MKMapRect annotationRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
    zoomRect = MKMapRectUnion(zoomRect, annotationRect);
    }
    UIEdgeInsets insets = UIEdgeInsetsMake(10, 10, 10, 10);
    [self.mapView setVisibleMapRect:zoomRect edgePadding:insets animated:YES];
    }
    }

Leave a Reply

Your email address will not be published.