gpt4 book ai didi

ios - 添加到mapkit时如何自动选择注释

转载 作者:行者123 更新时间:2023-12-01 17:39:49 25 4
gpt4 key购买 nike

我有一张 map ,我可以用代码在其中添加图钉(注释)。
我希望在运行我的应用程序时以及在我的应用程序中添加注释时自动选择此注释,当单击 map 而不是注释我的注释时取消选择。
我可以处理选择和取消选择我的注释,但肯定必须单击注释直到选择并单击 map (另一个地方而不是注释)直到我的注释取消选择。

我不想要这个。我希望在运行我的应用程序时自动选择我的注释,并且只需要单击 map 上的另一个位置,直到我的注释取消选择。

请指导我。这是我处理选择/取消选择注释的代码:

static NSString* const ANNOTATION_SELECTED_DESELECTED = @"mapAnnotationSelectedOrDeselected";


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
NSLog(@"2");
NSString *action = (__bridge NSString *)context;
if ([action isEqualToString:ANNOTATION_SELECTED_DESELECTED]) {
BOOL annotationSelected = [[change valueForKey:@"new"] boolValue];
if (annotationSelected) {
NSLog(@"Annotation was selected, do whatever required");
}else {
NSLog(@"Annotation was deselected, do what you must");

}
}
}
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
if ([mapViews.annotations count] > 1) {
for (MKAnnotationView *anAnnotationView in views) {
[anAnnotationView addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew context:(__bridge void *)(ANNOTATION_SELECTED_DESELECTED)];
}
}

}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"Prospects"];
if ([annotation isKindOfClass:[MKUserLocation class]]){
return nil; //return nil to use default blue dot view
}
else if(pinView == nil) {

pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Prospects"];
pinView.pinColor = MKPinAnnotationColorPurple;
pinView.animatesDrop = YES;
pinView.draggable = NO;
}
return pinView;
}

最佳答案

选择指定的注释并为其显示标注 View 。
[mapView selectAnnotation:pinView animated:YES];//这里pinView是你的注释,mapview是你的 map

如果指定的注释不在屏幕上,因此没有关联的注释 View ,则此方法无效。

单击 map 上的另一个地方,直到我的注释取消选择。
它是 mapview 的默认行为。

MKMapView Reference

opening-callout-of-annotation-on-map-automatically-when-map-first-load

关于ios - 添加到mapkit时如何自动选择注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23739659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com