2014-04-16 121 views
0

我想使用我的Pin(不是默认值)。MapKit注释的自定义引脚图像

我试过上面的代码。首先,我可以看到默认引脚。当我触摸showMyLocation时,在触碰sightLocation后,默认引脚会随着我的自定义引脚而改变。我想在第一个视图中显示我的自定义引脚。

TOCGSightAnnotation.h

@interface TOCGSightAnnotation : NSObject <MKAnnotation> 
@property (strong, nonatomic) NSString *title; 
@property (nonatomic,assign) CLLocationCoordinate2D coordinate; 
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title; 
@end 

TOCGSightAnnotation.m

@implementation TOCGSightAnnotation 

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title 
{ 
    if ((self = [super init])) { 
     self.coordinate =coordinate; 
     self.title = title; 
    } 
    return self; 
} 

@end 

TOCGSightseeingMapKitViewController.h

@interface TOCGSightseeingMapKitViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate> 

@property (strong) NSNumber *latitude; 
@property (strong) NSNumber *longitude; 
@property (weak, nonatomic) IBOutlet MKMapView *mapView; 
@property (strong, nonatomic) CLLocationManager * locationManager; 

@end 

TOCGSightseeingMapKitViewController.m

#import "TOCGSightseeingMapKitViewController.h" 
#import "TOCGSightAnnotation.h" 

CLLocationCoordinate2D sightCoordinate; 
MKCoordinateRegion region; 

@interface TOCGSightseeingMapKitViewController() 

@end 

@implementation TOCGSightseeingMapKitViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    self.locationManager = [[CLLocationManager alloc] init]; 
    self.locationManager.delegate = self; 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; 
    [self.locationManager startUpdatingLocation]; 

    sightCoordinate = CLLocationCoordinate2DMake([self.latitude doubleValue], [self.longitude doubleValue]); 

    TOCGSightAnnotation *annotation = [[TOCGSightAnnotation alloc] initWithCoordinate:sightCoordinate title:@"Sight Title"]; 
    [self.mapView addAnnotation:annotation]; 

    region = 
    MKCoordinateRegionMakeWithDistance(sightCoordinate, 500, 500); 

    [self.mapView setRegion:region animated:YES]; 
} 

- (IBAction)myLocation:(id)sender 
{ 
    self.mapView.delegate = self; 
    self.mapView.showsUserLocation = YES; 
    [self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES]; 
} 

- (IBAction)sightLocation:(id)sender 
{ 
    [self.mapView setRegion:region animated:YES]; 
} 

- (MKAnnotationView *)mapView:(MKMapView *)mapView 
      viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 

    if ([annotation isKindOfClass:[TOCGSightAnnotation class]]) 
    { 
     MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView 
                   dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"]; 

     if (!pinView) 
     { 
      pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation 
                 reuseIdentifier:@"CustomPinAnnotationView"]; 
      pinView.image = [UIImage imageNamed:@"mapIcon.png"]; 
      pinView.pinColor = MKPinAnnotationColorRed; 
      pinView.animatesDrop = YES; 
      pinView.canShowCallout = YES; 

     } 
     else 
      pinView.annotation = annotation; 

     return pinView; 
    } 

    return nil; 
} 

回答

1

原因很简单。方法

- (MKAnnotationView *)mapView:(MKMapView *)mapView 
      viewForAnnotation:(id <MKAnnotation>)annotation 

只有在设置MKMapViewDelegate时才会被调用。只要你没有触及按钮,调用IBAction

- (IBAction)myLocation:(id)sender 

委托没有设置,所以委托方法不会被调用。尝试移动线路

self.mapView.delegate = self; 

在您的MKMapView分配后。

+0

我加了self.mapView.delegate = self;第一行。现在,它始终是默认引脚。委托方法不被调用。 –

+0

添加一个断点,以查看在您设置委托的位置您的MKMapView是否为空。该行必须在创建MKMapView之后添加。 – Emilie

+0

我加了self.mapView.delegate = self;以viewDidAppear和它工作正常。感谢您的帮助。 –

0

我试过这个,它的工作原理。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    self.locationManager = [[CLLocationManager alloc] init]; 
    self.locationManager.delegate = self; 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; 
    [self.locationManager startUpdatingLocation]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    self.mapView.delegate = self; 

    sightCoordinate = CLLocationCoordinate2DMake([self.latitude doubleValue], [self.longitude doubleValue]); 

    TOCGSightAnnotation *annotation = [[TOCGSightAnnotation alloc] initWithCoordinate:sightCoordinate title:@"Sight Title"]; 
    [self.mapView addAnnotation:annotation]; 

    region = 
    MKCoordinateRegionMakeWithDistance(sightCoordinate, 500, 500); 

    [self.mapView setRegion:region animated:YES]; 
}