ios - 在 ios 中创建自定义标注

标签 ios cocoa-touch mkmapview mkannotationview

我想在我的 map 上创建自定义标注。我现在已经尝试过 -

    -(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
    NSLog(@"ANNNOTATION VIEW : %@", view);
    NSLog(@"VIEW ANNOTATION: %@", view.annotation);
    MyMapAnnotationViewController* mapAnnotationViewController = [[MyMapAnnotationViewController alloc]initWithNibName:@"MapAnnotationView" bundle:nil];
    MyLocation* location = (MyLocation*)view.annotation;
    [mapAnnotationViewController setTitle: [location title]];
    [mapAnnotationViewController setRating:3.0];
    [view addSubview:mapAnnotationViewController.view];   
}

-(void)viewWillAppear:(BOOL)animated{
    [_mapView setRegion: _viewRegion];
    for (id<MKAnnotation> annotation in _mapView.annotations) {
        [_mapView removeAnnotation:annotation];
    }
    for(NSDictionary* result in _resultsToPlot){
        NSString* address = someAddr;
        NSString* restaurantTitle = someTitle;
        NSString* description = someDescription;
        NSString* lonLat = someLonLat;
        NSArray *list = [lonLat componentsSeparatedByString:@";"];
        CLLocationCoordinate2D coordinate;
        coordinate.longitude = [[list objectAtIndex: 1] doubleValue];
        coordinate.latitude = [[list objectAtIndex: 0] doubleValue];
        MyLocation *annotation = [[MyLocation alloc] initWithName:restaurantTitle address:address coordinate:coordinate] ;

        [_mapView addAnnotation:annotation];
    }

MyLocation 是 MKAnnotation 的子类。

但是,当我点击 - 时,这就是现在的样子

enter image description here

因此,当我单击一个图钉时,会显示我的自定义 View 并显示注释。我只想显示自定义 View 。此外,当我单击另一个图钉时,以前的自定义 View 仍然存在。

如何获取它以使注释成为我的自定义 View ?

好的 - 所以我做了以下操作并添加了这个 -

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(MKAnnotationView*)annotation{
    annotation.canShowCallout = NO;
    return annotation;
}

我现在收到此错误-

NSInvalidArgumentException', reason: '-[MyLocation setCanShowCallout:]: unrecognized selector sent to instance 0xcb683e0'

最佳答案

要禁用内置标注的显示,请将 MKPinAnnotationView/MKAnnotationView 上的 canShowCallout 设置为 NOviewForAnnotation 委托(delegate)方法中。

下一个问题是,当前代码始终创建自定义标注 View 并将其添加到所选注释的 View 中。这就是为什么您会同时显示多个标注(之前选择的注释上的标注不会被删除)。

一种解决方案是保留自定义标注 View 的单个实例,而不是在每次选择注释时创建一个新实例。然后,只需在选择或取消选择注释时添加/删除标注 View 即可。

将此单个实例声明为包含 map View 的 View Controller 中的强属性:

@property (nonatomic, strong) MyMapAnnotationViewController* mapAnnotationViewController;

viewDidLoad(或viewWillAppear,如果这适合您)中创建标注实例,但此时不要将其添加为 subview - 只需创建它:

self.mapAnnotationViewController = [[MyMapAnnotationViewController alloc...

didSelectAnnotationView 中,不要创建新的标注 View 实例,而是告诉现有实例将其自身从当前的 super View (如果有)中删除,然后将其添加到新注释的 View 中:

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
    NSLog(@"ANNNOTATION VIEW : %@", view);
    NSLog(@"VIEW ANNOTATION: %@", view.annotation);

    [self.mapAnnotationViewController.view removeFromSuperview];

    MyLocation* location = (MyLocation*)view.annotation;
    [self.mapAnnotationViewController setTitle: [location title]];
    [self.mapAnnotationViewController setRating:3.0];
    //Since we are re-using the callout view, 
    //may need to do additional "cleanup" so that the callout
    //shows the new annotation's data.

    [view addSubview:self.mapAnnotationViewController.view];   
}

最后,您需要处理用户通过点击 map (而不是另一个注释)取消选择注释的情况,并仅删除标注 View :

-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
    [self.mapAnnotationViewController.view removeFromSuperview];
}


viewForAnnotation 委托(delegate)方法应如下所示:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if (! [annotation isKindOfClass:[MyLocation class]])
    {
        //tell map view to show default view for annotations other than ours
        //(like the user location blue dot)
        return nil;
    }

    MKPinAnnotationView *pav = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];
    if (pav == nil)
    {
        pav = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];
        pav.canShowCallout = NO;
    }
    else
    {
        pav.annotation = annotation;
    }

    return pav;
}

关于ios - 在 ios 中创建自定义标注,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20886625/

相关文章:

objective-c - Instruments VM Tracker 两次列出 Dirty

ios - 禁用背景颜色更改,但在 UITableViewCell 突出显示时启用文本颜色更改

android - CodenameOne:单击菜单项时出错

cocoa-touch - 修复启用 ARC 的代码中的警告 "Capturing [an object] strongly in this block is likely to lead to a retain cycle"

iphone - 更新 iOS 7 map 相机旋转的 map 注释

ios - 如何使用 NSAttributedString 在 Swift 中将图像添加为文本附件?

objective-c - 为什么断言不是NSFetchRequest的属性?

iphone - 选择的 UIButton 不起作用

cocoa - 使用 KVO 时出现异常

iphone - MKMapView 自动移动注释 - 为它们设置动画?