Я потратил много часов, пытаясь понять, как это сделать:
Имея метку/аннотацию в centerCoordinate вашего mapView, при прокрутке карты метка всегда должна оставаться в центре.
Я видел другое приложение, делающее это тоже!
Я потратил много часов, пытаясь понять, как это сделать:
Имея метку/аннотацию в centerCoordinate вашего mapView, при прокрутке карты метка всегда должна оставаться в центре.
Я видел другое приложение, делающее это тоже!
Нашел свой вопрос в Как добавить аннотацию в центр просмотра карты в iPhone?
Там ответ:
Если вы хотите использовать реальную аннотацию вместо обычного вида, расположенного над центром вида карты, вы можете:
MKPointAnnotation
). Это позволяет избежать необходимости удалять и добавлять аннотацию при изменении центра.regionDidChangeAnimated
представления карты (убедитесь, что свойство делегата представления карты установлено)Пример:
@interface SomeViewController : UIViewController <MKMapViewDelegate> {
MKPointAnnotation *centerAnnotation;
}
@property (nonatomic, retain) MKPointAnnotation *centerAnnotation;
@end
@implementation SomeViewController
@synthesize centerAnnotation;
- (void)viewDidLoad {
[super viewDidLoad];
MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.coordinate = mapView.centerCoordinate;
pa.title = @"Map Center";
pa.subtitle = [NSString stringWithFormat:@"%f, %f", pa.coordinate.latitude, pa.coordinate.longitude];
[mapView addAnnotation:pa];
self.centerAnnotation = pa;
[pa release];
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
centerAnnotation.coordinate = mapView.centerCoordinate;
centerAnnotation.subtitle = [NSString stringWithFormat:@"%f, %f", centerAnnotation.coordinate.latitude, centerAnnotation.coordinate.longitude];
}
- (void)dealloc {
[centerAnnotation release];
[super dealloc];
}
@end
Теперь это будет перемещать аннотацию, но не плавно. Если вам нужно, чтобы аннотация перемещалась более плавно, вы можете добавить UIPanGestureRecognizer
и UIPinchGestureRecognizer
в представление карты, а также обновить аннотацию в обработчике жестов:
// (Also add UIGestureRecognizerDelegate to the interface.)
// In viewDidLoad:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
panGesture.delegate = self;
[mapView addGestureRecognizer:panGesture];
[panGesture release];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
pinchGesture.delegate = self;
[mapView addGestureRecognizer:pinchGesture];
[pinchGesture release];
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
centerAnnotation.coordinate = mapView.centerCoordinate;
centerAnnotation.subtitle = [NSString stringWithFormat:@"%f, %f", centerAnnotation.coordinate.latitude, centerAnnotation.coordinate.longitude];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
//let the map view's and our gesture recognizers work at the same time...
return YES;
}