我有两个视图控制器,MapViewVC
和MapDetailViewVC
。
我在MapView
上有几个自定义图钉注释。
点击时,这些注释(具有某个默认的“高度”视图)会启动MapDetailVC
,显示注释的快照,并将相机属性设置为确定的高度(4000米)。
因此,当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];
答案 0 :(得分: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;
}
以上代码取自此 answer