我认为我没有正确理解这一点,我在地图上添加了注释。我看得出来,颜色是红色的。但是,我想将其更改为紫色,并希望它作为一个按钮。所以我把它放在下面:
// 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委托方法??如果这是错误的,请纠正我的理解。我如何解决这个问题,以便我可以更改颜色并添加一个按钮。
发布于 2010-10-29 10:56:02
将一个或多个注释添加到地图时,将触发didAddAnnotationViews方法。您可以更新UI的其他部分或执行其他逻辑,知道注释已“就绪”。它不一定是更新注释外观的地方。
在viewForAnnotation方法中,您可以了解批注的外观。它类似于表视图中的cellForRowAtIndexPath方法。
当您运行问题中的代码时会发生什么?为什么不在viewForAnnotation方法中设置紫色的pinColor和附件,而忘记didAddAnnotationViews方法呢?
还要注意,在didAddAnnotationViews中,视图数组包含MKAnnotationView对象,而不仅仅是MKPinAnnotationView对象。因此,如果其中一个注释视图不是MKPinAnnotationView,则设置pinColor的行将崩溃。
编辑:
即使实现了这些将引脚颜色设置为其他颜色的方法,你的引脚也会显示为红色。最可能的原因是没有设置mapView的委托。在viewDidLoad中,在调用addAnnotation之前,请说:
mapView.delegate = self;然后,您应该删除didAddAnnotationViews方法,并将pinColor设置为紫色,然后在viewForAnnotation中添加附件按钮。
https://stackoverflow.com/questions/4048463
复制相似问题