Ios 将自定义注释添加到mapView时自动调用didSelectAnnotationView
我正在尝试在MKMapView上添加自定义注释,并在点击注释时实现自定义详图索引视图 我下面的链接是相同的。问题在于,当我添加自定义注释时,didSelectAnnotationView会被单独调用,并且会显示popOver标注,即使用户尚未单击注释 这是我的密码:Ios 将自定义注释添加到mapView时自动调用didSelectAnnotationView,ios,mkmapview,mkannotationview,Ios,Mkmapview,Mkannotationview,我正在尝试在MKMapView上添加自定义注释,并在点击注释时实现自定义详图索引视图 我下面的链接是相同的。问题在于,当我添加自定义注释时,didSelectAnnotationView会被单独调用,并且会显示popOver标注,即使用户尚未单击注释 这是我的密码: -(void)callAddAnnotationsLocal{ [_mapView removeAnnotations:[_mapView annotations]]; CLLocationCoordina
-(void)callAddAnnotationsLocal{
[_mapView removeAnnotations:[_mapView annotations]];
CLLocationCoordinate2D coord = {.latitude = 43.3998170679, .longitude = -95.1288472486};
[_mapView addAnnotations:[self annotationsLocal]];
}
-(NSArray *)annotationsLocal{
self.annotArrayLocal = nil;
self.annotArrayLocal = [[NSMutableArray alloc] init];
for (int i=0; i<[arrAmmenities count];i++)
{
MKAnnotationView *propertyAnnotation = [[MKAnnotationView alloc] init];
UIFont *font = [UIFont fontWithName:@"Arial" size:18];
NSDictionary *userAttributes = @{NSFontAttributeName: font,
NSForegroundColorAttributeName: [UIColor blackColor]};
NSString *latStr = [NSString stringWithFormat:@"%@",[[arrAmmenities objectAtIndex:i]objectForKey:@"latitude"]];
float latitude = [latStr floatValue];
NSString *longStr = [NSString stringWithFormat:@"%@",[[arrAmmenities objectAtIndex:i]objectForKey:@"longitude"]];
float longitude = [longStr floatValue];
CLLocationCoordinate2D coord = {.latitude = latitude, .longitude = longitude};
PinAnnotation * annotation = [[PinAnnotation alloc] init];
[annotation setCoordinate:coord];
[annotation setType:[[arrAmmenities objectAtIndex:i] objectForKey:@"category"]];
[annotation setTag:i];
[self.mapView addAnnotation:annotation];
[self.annotArrayLocal addObject:propertyAnnotation];
}
return _annotArrayLocal;
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation {
MKAnnotationView *annotationView;
NSString *identifier;
if ([annotation isKindOfClass:[PinAnnotation class]]) {
// Pin annotation.
identifier = @"Pin";
annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
if([[(PinAnnotation *)annotation type] isEqualToString:@"Gas Stations"])
annotationView.image = [UIImage imageNamed:@"marker_gas"];
else if([[(PinAnnotation *)annotation type] isEqualToString:@"Golf Courses"])
annotationView.image = [UIImage imageNamed:@"marker_golf"];
else if([[(PinAnnotation *)annotation type] isEqualToString:@"Restaurants"])
annotationView.image = [UIImage imageNamed:@"marker_restaurant"];
else
annotationView.image = [UIImage imageNamed:@"marker_hospital"];
annotationView.canShowCallout = NO;
}
return annotationView;
}
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
// [mapView deselectAnnotation:view.annotation animated:YES];
MapAnnotViewController *controller = [[UIStoryboard storyboardWithName:@"MapStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"sidMapAnnotVC"];
// controller.annotation = view.annotation; // it's useful to have property in your view controller for whatever data it needs to present the annotation's details
wyPopController = [[WYPopoverController alloc] initWithContentViewController:controller];
wyPopController.delegate = self;
[wyPopController setPopoverContentSize:CGSizeMake(200.0, 100.0)];
[wyPopController presentPopoverFromRect:view.frame
inView:view.superview
permittedArrowDirections:WYPopoverArrowDirectionAny
animated:YES];
}
我哪里做错了?我该如何解决这个问题?一些想法:
在annotationsLocal中,不仅要实例化PinAnnotation对象并将其添加到地图中,还要实例化MKAnnotationView对象,构建它们的数组,然后返回该数组,然后将其作为注释添加到地图视图中
MKAnnotationView的第二个数组根本不应该构建,当然也不应该添加为地图视图的注释。不要将表示地图上点坐标的注释对象与表示这些注释在地图上渲染时的视觉表示的注释视图对象合并
另外,与您的问题无关,您也在viewForAnnotation中实例化不必要的注释视图。您应该将注释视图出列,并且只有在未成功检索到要重用的注释视图时才实例化新的注释视图:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation {
MKAnnotationView *annotationView;
if ([annotation isKindOfClass:[PinAnnotation class]]) {
// Pin annotation.
NSString *identifier = @"Pin";
annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView) {
annotationView.annotation = annotation
} else {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.canShowCallout = NO;
}
if ([[(PinAnnotation *)annotation type] isEqualToString:@"Gas Stations"])
annotationView.image = [UIImage imageNamed:@"marker_gas"];
else if([[(PinAnnotation *)annotation type] isEqualToString:@"Golf Courses"])
annotationView.image = [UIImage imageNamed:@"marker_golf"];
else if([[(PinAnnotation *)annotation type] isEqualToString:@"Restaurants"])
annotationView.image = [UIImage imageNamed:@"marker_restaurant"];
else
annotationView.image = [UIImage imageNamed:@"marker_hospital"];
}
return annotationView;
}
但我在这里没有看到任何会导致调用didSelectAnnotationView的东西,除非添加注释视图作为注释的错误过程产生了一些奇怪的副作用。如果您仍然看到调用了didSelectAnnotationView,我可能建议在那里添加一个断点,查看调用堆栈,看看您是否可以看到调用的位置。但希望注释SLOCAL的修复能够修复它