2011-10-14 56 views
3

我似乎無法改變引腳顏色。MapKit mapView:viewForAnnotation對引腳顏色沒有影響

我有我的視圖控制器延伸<MKMapViewDelegate>和執行mapView:viewForAnnotation我很近,但必須缺少的東西。任何幫助,將不勝感激。

MainViewController.h

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

#define METERS_PER_MILE 1609.344 

@interface MainViewController : UIViewController <MKMapViewDelegate> { 

} 
@property (weak, nonatomic) IBOutlet MKMapView *mapView; 

@end 

MapViewController.m

#import "MainViewController.h" 

@implementation MainViewController 
@synthesize mapView=_mapView; 

- (void)viewWillAppear:(BOOL)animated 
{ 
    CLLocationCoordinate2D zoomLocation; 
    zoomLocation.latitude = 43.066667; 
    zoomLocation.longitude = -89.4; 

    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, METERS_PER_MILE, METERS_PER_MILE); 

    MKCoordinateRegion adjustRegion = [_mapView regionThatFits:viewRegion]; 

    [_mapView setRegion:adjustRegion animated:YES]; 
    [_mapView addAnnotation:[[StopAnnotation alloc] initWithCoordinate:zoomLocation]]; 
} 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { 
    MKPinAnnotationView *pav = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil]; 
    pav.pinColor = MKPinAnnotationColorPurple; 
    return pav; 
} 

// the rest of the methods are default, i.e. viewDid* and shouldAutorotate*, etc... 

StopAnnotation.h

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

@interface StopAnnotation : NSObject <MKAnnotation> { 
    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subtitle; 
} 
- (id)initWithCoordinate:(CLLocationCoordinate2D)c; 
@end 

StopAnnotation.m

#import "StopAnnotation.h" 

@implementation StopAnnotation 
@synthesize coordinate; 

- (NSString *)subtitle { 
    return @"subtitle"; 
} 

- (NSString *)title { 
    return @"title"; 
} 

- (id)initWithCoordinate:(CLLocationCoordinate2D)c { 
    coordinate = c; 
    NSLog(@"%f,%f", c.latitude, c.longitude); 
    return self; 
} 

@end 

我在做練習&代碼主要是從here

謝謝!

+0

那麼你的針腳不是紫色的? – jbat100

+0

@ jbat100是啊,他們仍然是紅色的:( – chizzle

回答

2

您必須設置MainViewController爲MapView類的代表,也就是說,如果你不是那麼的地圖視圖可能會生成默認引腳,如果你把在

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

斷點它實際上被調用?

+0

你是對的,它似乎並沒有被調用,我將如何去設置代理(以及在哪個類)? – chizzle

+0

哈哈我在故事板&它工作!謝謝一堆!故事板數據曾經寫在代碼或只是內部數據?MainViewController中的 – chizzle

+1

viewDidLoad:通過做mapView.delegate = self設置委託您需要聲明MainViewController作爲解密到MKMapViewDelegate協議沉默編譯器警告 – jbat100