我可以看到我的NSLog输出,因此调用了我的MKAnnotationView委托方法。但是,图钉未出现在地图上。我在这里想念什么吗?

MapViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MapViewController : UIViewController <MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *nearbyMapView;
@end

MapViewController.m
#import "MapViewController.h"
#import "AppDelegate.h"

@interface MapViewController ()

@end

@implementation MapViewController
AppDelegate *appDelegate;

- (void)viewDidLoad
{
    [super viewDidLoad];
    appDelegate=[[UIApplication sharedApplication] delegate];
    CLLocationCoordinate2D center = CLLocationCoordinate2DMake(54.995184, -1.566699);
    MKCoordinateSpan span = MKCoordinateSpanMake(0.5, 0.5);
    MKCoordinateRegion regionToDisplay = MKCoordinateRegionMake(center, span);
    [self.nearbyMapView setRegion: regionToDisplay];

    for (int i = 0; i < [[appDelegate offersFeeds] count]; i++) {
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        NSString *plotAddress = [[[appDelegate offersFeeds] objectAtIndex:i] valueForKey:@"addressline"];
        NSString *plotTitle = [[[appDelegate offersFeeds] objectAtIndex:i] valueForKey:@"title"];

        [geocoder geocodeAddressString:plotAddress completionHandler:^(NSArray *placemarks, NSError *error) {
            if (placemarks && placemarks.count > 0)
            {
                CLPlacemark *topResult = [placemarks objectAtIndex:0];
                MKPlacemark *placemark = [[MKPlacemark alloc]initWithPlacemark:topResult];

                MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
                pa.coordinate = placemark.location.coordinate;
                pa.title = plotTitle;

                [self.nearbyMapView addAnnotation:pa];
            }
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    MKAnnotationView *pinView = nil;

    static NSString *defaultPinID = @"identifier";
    pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinView == nil ) {
        NSLog(@"Inside IF");
        pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
        pinView.enabled = YES;
        pinView.canShowCallout = YES;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        //Accessoryview for the annotation view in ios.
        pinView.rightCalloutAccessoryView = btn;
    }
    else {
        pinView.annotation = annotation;
    }
    pinView.annotation = annotation;
    return pinView;
}

@end

最佳答案

viewForAnnotation中,代码正在创建MKAnnotationView,但未为其设置imageimage上没有默认的MKAnnotationView,因此注释不可见。

当您根本不实现委托时,地图视图会为您创建带有红色大头针颜色的MKPinAnnotationViewMKPinAnnotationViewMKAnnotationView的便捷子类,可提供大头针图像(三种颜色之一)。

在实现委托时,由您来创建正确的视图并根据需要设置属性。

要么创建一个MKPinAnnotationView(提供默认的针脚图像),要么在普通image上设置MKAnnotationView属性。

要使用MKPinAnnotationView:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id             <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinView = nil;

    static NSString *defaultPinID = @"identifier";
    pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinView == nil )
    {
        NSLog(@"Inside IF");
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];

        pinView.pinColor = MKPinAnnotationColorRed;  //or Green or Purple

        pinView.enabled = YES;
        pinView.canShowCallout = YES;

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

        //Accessoryview for the annotation view in ios.
        pinView.rightCalloutAccessoryView = btn;
    }
    else
    {
        pinView.annotation = annotation;
    }

    return pinView;
}

或使用MKAnnotationView和您自己的image:
//same code as the current but add this line
//after the initWithAnnotation:
pinView.image = [UIImage imageNamed:@"SomeImage.png"];

关于ios - 使用MKAnnotationView委托(delegate)时,针脚消失了,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19640858/

10-13 04:28