Returning previous MKPinAnnotation coordinate from MKMapSnapShot

107 Views Asked by At

I have two view controllers, MapViewVC and MapDetailViewVC.

I have several custom pin annotations on the MapView.

When tapped, these annotations (with a certain default "altitude" view) initiate a the MapDetailVC that shows a snapshot of the annotation with the camera property set to a definite altitude (4000 m).

As a result, when the "back" button is pressed on the MapDetailVC, the view goes back to the MapViewVC with same altitude as was presented in the MapDetailVC; not to the original altitude which was simply the area being viewed at the time the callout button was tapped.

I want to know (from those more experienced with MapKit) if there is a way to set the mapView back to its original setting when the "back" button is tapped.

Thanks

MapDetailViewController *mapDetail = [[self storyboard]
instantiateViewControllerWithIdentifier:@"MapDetailViewController"];

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Map"
                                                               style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;

Word *word = [[Word alloc] init];
word.name = biblicalPin.title;


MKMapCamera  *myCamera = [MKMapCamera
                          cameraLookingAtCenterCoordinate:biblicalPin.coordinate
                          fromEyeCoordinate:biblicalPin.coordinate
                          eyeAltitude:2000];

mapView.camera = myCamera;


MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
options.size = CGSizeMake(320, 140);
options.camera = myCamera;
options.scale = [[UIScreen mainScreen] scale]; 
options.region = self.mapView.region;
options.mapType = MKMapTypeSatellite;

MKMapSnapshotter *snapshotter =
[[MKMapSnapshotter alloc] initWithOptions:options];
[snapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *e)
 {
 //if (e) ...;// Handle errors

 UIImage *image = snapshot.image;

 mapDetail.imageView.image = image;
 mapDetail.currentWordDetail = word;
 mapDetail.locationLabel.text = biblicalPin.title;
 mapDetail.locationDescription.text = biblicalPin.information;
 //[backButton --- add a method to return the user to the original mapView alititude.

 }];

word.definition = biblicalPin.information;

[self.navigationController pushViewController:mapDetail animated:YES];
1

There are 1 best solutions below

1
On

One way to reset map is call delegate method when back button is pressed.

Create delegate method in MapDetailVC like:

@protocol MapDetailDelegate <NSObject>

- (void) resetMapView;

@end

@interface MapDetailVC : UIViewController

@property (nonatomic, weak) id <MapDetailDelegate>    delegate;

@end

Make delegate self when you create view controller object in MapViewVC controller class. Also extend extend MapViewVC class interface with delegate <MapDetailDelegate>

MapDetailViewController *mapDetail = [[self storyboard]
instantiateViewControllerWithIdentifier:@"MapDetailViewController"];
mapDetail.delegate = self;

Create method -resetMapView() in MapViewVC class.

- (void) resetMapView {
    // Reset map. Default settings.

    // Reset map to user current location.
    [UIView animateWithDuration:1.5 animations:^{

        [self.mapView setCenterCoordinate:self.mapView.userLocation.coordinate];

    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1.5 animations:^{
            MKCoordinateSpan span;
            span.latitudeDelta  = 1;
            span.longitudeDelta = 1;

            MKCoordinateRegion region;
            region.span = span;
            region.center = self.mapView.userLocation.coordinate;
            [self.mapView setRegion:region animated:YES];
        }];
    }];
}

Now when back button is pressed call this method from MapDetailVC controller class using delegate object.

- (void) backButtonTapped:(id)sender {
    [self.delegate resetMapView];

    [self.navigationController popViewControllerAnimated:YES];
}

Edit:

The flow for reset map here will be same for your need. Now instead of getting user location you can call below method in -resetMapView() to load default camera zoom.

-(CLLocationCoordinate2D)translateCoord:(CLLocationCoordinate2D)coord MetersLat:(double)metersLat MetersLong:(double)metersLong{

    CLLocationCoordinate2D tempCoord;

    MKCoordinateRegion tempRegion = MKCoordinateRegionMakeWithDistance(coord, metersLat, metersLong);
    MKCoordinateSpan tempSpan = tempRegion.span;

    tempCoord.latitude = coord.latitude + tempSpan.latitudeDelta;
    tempCoord.longitude = coord.longitude + tempSpan.longitudeDelta;

    return tempCoord;    
}

Above code is taken from this answer