use*_*133 1 annotations objective-c mapkit mkannotation ios
我有一张地图,我可以用代码添加pin(注释).我想在运行我的应用程序时,在我的应用程序中添加注释时自动选择此注释,当单击地图而不是注释我的注释取消选择.我可以处理选择并取消选择我的注释,但肯定必须单击注释,直到选择并单击地图(另一个地方而不是注释),直到我的注释取消选择.
我不想要这个.我想在运行我的应用时自动选择我的注释,只需要在地图上点击另一个地方,直到我的注释取消选择.
请指导我.这是我处理select/deselect注释的代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
选择指定的注释并为其显示标注视图.
[mapView selectAnnotation:pinView animated:YES]; //这里pinView是你的注释,mapview是你的地图
如果指定的注释不在屏幕上,因此没有关联的注释视图,则此方法无效.
单击地图上的其他位置,直到我的注释取消选择. 它是mapview的默认行为.
| 归档时间: |
|
| 查看次数: |
4581 次 |
| 最近记录: |