I have a database which stores latitude and longtitude values. I want to iterate through the list and show them on the map. I have written a condition that says if the type is atm then the annotation color should be green and if it is branch it should show red.

if ([myNewArrayElement isEqualToString:@"BRANCH"]) {                   
    self.customAnnotation = [[BasicMapAnnotation alloc] initWithLatitude:[[record valueForKey:@"lat"]floatValue] andLongitude:[[record valueForKey:@"lon"]floatValue]] ;
    NSLog(@"Current coordinates%f%f",[[record valueForKey:@"lat"]floatValue],[[record valueForKey:@"lon"]floatValue]);
    mPinColor = @"PURPLE";

    self.customAnnotation.title = record.type;
    MKPinAnnotationView * annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:self.customAnnotation
                                                              reuseIdentifier:@"CustomAnnotation"] ;
    annotationView.canShowCallout = YES;
    mPinColor = @"";
    NSLog(@"Its Basic");
    annotationView.pinColor = MKPinAnnotationColorGreen;

    [self.mapView addAnnotation:self.customAnnotation];
    self.mapView.delegate = self;
}
else if ([myNewArrayElement isEqualToString:@"ATM"]) {
    self.normalAnnotation = [[BasicMapAnnotation alloc] initWithLatitude:[[record valueForKey:@"lat"]floatValue] andLongitude:[[record valueForKey:@"lon"]floatValue]] ;
    mPinColor = @"GREEN";
    self.normalAnnotation.title = record.type;
    MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:self.normalAnnotation
                                                               reuseIdentifier:@"NormalAnnotation"] ;
   annotationView.canShowCallout = YES;
   mPinColor = @"";
   NSLog(@"Its Basic");
   annotationView.pinColor = MKPinAnnotationColorGreen;
   [self.mapView addAnnotation:self.normalAnnotation];

}

My viewForAnnotation is

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

    NSLog(@"Length of mpincolor%d",mPinColor.length);
    MKPinAnnotationView *annotationView;
    if ([mPinColor isEqualToString:@"PURPLE"]) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomAnnotation"] ;
        annotationView.canShowCallout = NO;
        NSLog(@"Its custom");
        annotationView.pinColor = MKPinAnnotationColorPurple;
        return annotationView;
    }
    else if([mPinColor isEqualToString:@"GREEN"]) {

       annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                             reuseIdentifier:@"NormalAnnotation"] ;
       annotationView.canShowCallout = YES;
       mPinColor = @"";
       NSLog(@"Its Basic");
       annotationView.pinColor = MKPinAnnotationColorGreen;

       return annotationView;
    }
    return nil;
}

I'm able to show the pins correctly, but the color is not showing properly. What am I doing wrong here?

有帮助吗?

解决方案

Add a color property to the BasicMapAnnotation class. Query that property in viewForAnnotation to properly initialize the pinColor. Btw, the code you use to create annotation views outside the viewForAnnotation method doesn't make much sence as the created views aren't used.

其他提示

i recommend you to visit this site and get benefited http://code4app.net/ios/Cluster-Annotations-MapView/4f7ffde206f6e7177a000000

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top