Ios MKPointAnnotation-won';不要改变颜色
我需要更改Ios MKPointAnnotation-won';不要改变颜色,ios,mkpointannotation,Ios,Mkpointannotation,我需要更改MKPointAnnotation对象的颜色,但我编写的方法似乎只生成红色PIN。该方法工作正常,但当我调用函数,传递给定常量之一的参数时,地图上显示的所有管脚都是红色(默认值)。有什么想法吗?代码如下 -(MKAnnotationView*) returnPointView: (CLLocationCoordinate2D) location andTitle: (NSString*) title andColor: (int) color{ /*Method
MKPointAnnotation
对象的颜色,但我编写的方法似乎只生成红色PIN。该方法工作正常,但当我调用函数,传递给定常量之一的参数时,地图上显示的所有管脚都是红色(默认值)。有什么想法吗?代码如下
-(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;
}
}
您的返回值可能错误…您返回的是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;
}
}
您必须遵守ViewController标题中的MKMapViewDelegate协议
@interface mapViewController : UIViewController <MKMapViewDelegate>
@end
我建议您看看Xcode中的MapCallout示例项目,它清晰而简单
同样,为了提高内存效率,您应该使用dequeueReusableAnnotationViewWithIdentifier
(如示例所示)
- (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;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.mapView setDelegate:self];
}