2

私の 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
4

1 に答える 1

11

ではviewForAnnotation、コードは を作成していますが、 をMKAnnotationView設定していませんimage。にはデフォルトがないimageためMKAnnotationView、注釈は表示されません。

デリゲートをまったく実装しない場合、マップ ビューはMKPinAnnotationView赤いピンの色で を作成します。 ピン画像 (3 色のいずれか) を提供MKPinAnnotationViewする便利なサブクラスです。MKAnnotationView

デリゲートを実装するときは、適切なビューを作成し、必要に応じてプロパティを設定する必要があります。

MKPinAnnotationView代わりに (デフォルトの pin イメージを提供する) を作成するか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"];
于 2013-10-28T17:22:31.510 に答える