1

我有两个视图控制器,MapViewVC并且MapDetailViewVC.

我在MapView.

当点击时,这些注释(具有特定的默认“高度”视图)会启动一个MapDetailVC显示注释的快照,其中相机属性设置为确定的高度(4000 m)。

结果,当按下 上的“返回”按钮时MapDetailVC,视图将返回到MapViewVC与 ; 中显示的高度相同的高度MapDetailVC。不是原始高度,这只是点击标注按钮时正在查看的区域。

我想知道(从那些更有经验的人MapKit那里)是否有办法在mapView点击“返回”按钮时将返回设置为原始设置。

谢谢

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];
4

1 回答 1

0

重置地图的一种方法是在按下后退按钮时调用委托方法。

像这样创建委托方法MapDetailVC

@protocol MapDetailDelegate <NSObject>

- (void) resetMapView;

@end

@interface MapDetailVC : UIViewController

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

@end

MapViewVC当您在控制器类中创建视图控制器对象时,请自行委托。MapViewVC还使用委托扩展扩展类接口<MapDetailDelegate>

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

-resetMapView()MapViewVC类中创建方法。

- (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];
        }];
    }];
}

现在,当按下后退按钮时,使用对象从MapDetailVC控制器类调用此方法。delegate

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

    [self.navigationController popViewControllerAnimated:YES];
}

编辑:

此处重置地图的流程将与您的需要相同。-resetMapView()现在,您可以调用以下方法来加载默认相机缩放,而不是获取用户位置。

-(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;    
}

上面的代码取自这个答案

于 2015-01-05T06:43:23.577 回答