简体   繁体   English

MKA注释问题

[英]MKAnnotation problems

I don't think I am understanding this correctly, I added the annotation to the map. 我认为我无法正确理解,因此我在地图上添加了注释。 I can see it, the color is red. 我看到了,颜色是红色。 However, I want to change it to purple and I want it as a button. 但是,我想将其更改为紫色,并希望将其作为按钮。 So I put it as follows: 所以我把它写成如下:

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    //[self generateAnnotation];
    CLLocationCoordinate2D aCoordinate;
    aCoordinate.latitude = 32.224023;
    aCoordinate.longitude = -110.965159;
    MapAnnotation * an1 = [[[MapAnnotation alloc] initWithCoordinate: aCoordinate] autorelease];
    an1.title = @"Party at Malloneys";
    an1.subtitle = @"213 N. 4th Ave";
    //[annotationArray addObject:an1];
    [mapView addAnnotation:an1];

    self.locationManager = [[[CLLocationManager alloc] init] autorelease];

    self.locationManager.delegate=self;
        self.locationManager.distanceFilter = kCLLocationAccuracyBest;
        [self.locationManager startUpdatingLocation];

}



- (void) mapView:(MKMapView *) mapView
    didAddAnnotationViews:(NSArray *) views
{
    for (MKPinAnnotationView * mkaview in views)
    {

        mkaview.pinColor = MKPinAnnotationColorPurple;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        mkaview.rightCalloutAccessoryView = button;
    }

}




- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    MKPinAnnotationView * annView = (MKPinAnnotationView*) [mapView dequeueReusableAnnotationViewWithIdentifier:@"an1"];
    if (!annView)
    {
        annView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"an1"] autorelease];
    annView.pinColor = MKPinAnnotationColorGreen;
    annView.animatesDrop = YES;
    annView.canShowCallout = YES;
    }
     else {
         annView.annotation = annotation;
     }

    return annView;
}   

I don't understand the two delegates above, viewForAnnotation is supposed for setting the view for the annotation right? 我不理解上面的两个委托,viewForAnnotation应该用于设置注释的视图吗? and the didAddAnnotationViews delegate method is executed after the view is loaded?? 并在加载视图后执行didAddAnnotationViews委托方法? Please correct my understanding if this is wrong. 如果这是错误的,请更正我的理解。 And how can I fix this so that I can change color and add a button. 以及如何解决此问题,以便可以更改颜色并添加按钮。

The didAddAnnotationViews method fires when one or more annotations are added to a map. 将一个或多个注释添加到地图时,将触发didAddAnnotationViews方法。 You can update other parts of your UI or do other logic knowing that the annotations are "ready". 您可以更新UI的其他部分,也可以通过执行其他逻辑来知道注释已“就绪”。 It's not necessarily a place to update the appearance of an annotation. 它不一定是更新注释外观的地方。

The viewForAnnotation method is where you tell how an annotation looks. 在viewForAnnotation方法中,您可以知道注释的外观。 It's like the cellForRowAtIndexPath method in the table view. 就像表格视图中的cellForRowAtIndexPath方法一样。

What happens when you run the code in your question? 当您运行问题中的代码时会发生什么? Why don't you just set the purple pinColor and accessory in the viewForAnnotation method and forget about the didAddAnnotationViews method? 您为什么不只在viewForAnnotation方法中设置紫色pinColor和附件,而忘了didAddAnnotationViews方法呢?

Also note that in didAddAnnotationViews, the views array contains MKAnnotationView objects and not just MKPinAnnotationView objects. 还要注意,在didAddAnnotationViews中,views数组包含MKAnnotationView对象,而不仅仅是MKPinAnnotationView对象。 So if one of the annotation views is not a MKPinAnnotationView, the line setting the pinColor will crash. 因此,如果注释视图之一不是MKPinAnnotationView,则设置pinColor的行将崩溃。

Edit: 编辑:
Your pins are coming up red even with these methods implemented that set the pin color to something else. 即使实现了将引脚颜色设置为其他颜色的方法,您的引脚也会变成红色。 Most likely cause is that the mapView's delegate is not set. 最可能的原因是未设置mapView的委托。 In viewDidLoad, before you call addAnnotation say: 在viewDidLoad中,在调用addAnnotation之前,请说:

mapView.delegate = self;

You should then remove the didAddAnnotationViews method and set the pinColor to purple and add the accessory button in viewForAnnotation. 然后,您应该删除didAddAnnotationViews方法,并将pinColor设置为紫色,然后在viewForAnnotation中添加附件按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM