我不認爲我正確理解這一點,我將註釋添加到地圖中。我可以看到它,顏色是紅色的。但是,我想將其更改爲紫色,並且希望將其作爲按鈕。所以我把它如下:MKAnnotation問題
// 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;
}
我不明白上面的兩名代表,viewForAnnotation應該用於設置註釋正確的看法?並且在加載視圖之後執行didAddAnnotationViews委託方法?如果這是錯誤的,請糾正我的理解。我該如何解決這個問題,以便我可以改變顏色並添加一個按鈕。
我曾嘗試刪除didAddAnnotationViews,並且仍然是紅色的針腳 – aherlambang 2010-10-29 03:25:39
最有可能的原因是mapView的代表未設置。在調用addAnnotation之前,在viewDidLoad中,說'mapView.delegate = self;'。 – Anna 2010-10-29 03:33:45
似乎viewForAnnotation沒有加載/執行/解僱。我只是試着在裏面放一個NSLog,它不起作用。爲什麼??? – aherlambang 2010-10-29 03:37:47