4

我需要更改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;
            }

        }
4

2 回答 2

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以提高内存效率(如示例所示)。

于 2013-10-01T23:50:47.520 回答
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;
        }

    }
于 2013-10-01T22:33:44.970 回答