我需要更改MKPointAnnotation
对象的颜色,但我写的方法似乎只生成红色引脚。该方法工作正常,但是当我调用该函数时,传递给定常量之一的参数,地图上显示的所有引脚都是红色(默认值)。有什么想法吗?代码如下。
-(MKAnnotationView*) returnPointView: (CLLocationCoordinate2D) location andTitle: (NSString*) title andColor: (int) color{
/*Method that acts as a point-generating machine. Takes the parameters of the location, the title, and the color of the
pin, and it returns a view that holds the pin with those specified details*/
MKPointAnnotation *resultPin = [[MKPointAnnotation alloc] init];
MKPinAnnotationView *result = [[MKPinAnnotationView alloc] initWithAnnotation:resultPin reuseIdentifier:Nil];
[resultPin setCoordinate:location];
resultPin.title = title;
result.pinColor = color;
return result;
}
//Function that calls above method
for(Party *party in allParties){
if(!party.alreadyAdded){
CLLocationCoordinate2D location = [party getPartylocation];
NSString *partyTitle = [party getPartyName];
MKAnnotationView *partyPin = [self returnPointView:location andTitle:partyTitle andColor:MKPinAnnotationColorGreen];
[self.map addAnnotation:partyPin.annotation];
NSLog(@"Adding Successful!");
party.alreadyAdded = YES;
}
}
答案 0 :(得分:4)
您必须在ViewController的标题中符合MKMapViewDelegate协议。
@interface mapViewController : UIViewController <MKMapViewDelegate>
@end
然后,在实现中你必须编写方法:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
每次绘制注释时都会调用它。在这里你应该调用你的方法,而不仅仅是在添加注释时。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pinView = [self returnPointView:annotation.coordinate andTitle:annotation.title andColor:MKPinAnnotationColorGreen];
return pinView;
}
最后,在ViewDidLoad中将ViewController设置为其UIMapView委托委托:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.mapView setDelegate:self];
}
我建议您查看Xcode中的MapCallouts示例项目,它清晰简单。
同样,您应该使用dequeueReusableAnnotationViewWithIdentifier
以提高内存效率(例如)。
答案 1 :(得分:3)
您的返回值可能有误...您正在返回MKAnnotationView而不是MKPinAnnotationView ... MKAnnotationView没有名为pinColor的属性..这应该可行
-(MKPinAnnotationView*) returnPointView: (CLLocationCoordinate2D) location andTitle: (NSString*) title andColor: (int) color{
/*Method that acts as a point-generating machine. Takes the parameters of the location, the title, and the color of the
pin, and it returns a view that holds the pin with those specified details*/
MKPointAnnotation *resultPin = [[MKPointAnnotation alloc] init];
MKPinAnnotationView *result = [[MKPinAnnotationView alloc] initWithAnnotation:resultPin reuseIdentifier:Nil];
[resultPin setCoordinate:location];
resultPin.title = title;
result.pinColor = color;
return result;
}
//Function that calls above method
for(Party *party in allParties){
if(!party.alreadyAdded){
CLLocationCoordinate2D location = [party getPartylocation];
NSString *partyTitle = [party getPartyName];
MKPinAnnotationView *partyPin = [self returnPointView:location andTitle:partyTitle andColor:MKPinAnnotationColorGreen];
[self.map addAnnotation:partyPin.annotation];
NSLog(@"Adding Successful!");
party.alreadyAdded = YES;
}
}