0

I have MKMap with around 190 custom pins. I would like to change pin image according to the boolean values. But I have made static boolean and in -(void)Map, I get data from Parse and also all boolean values. When I use If/Else in delegate method for custom pins, all booleans went FALSE.

-(void)Map{
    CLLocation *userLoc = self.mapView.userLocation.location;
    CLLocationCoordinate2D userCoordinate = userLoc.coordinate;

    NSLog(@"user latitude = %f",userCoordinate.latitude);
    NSLog(@"user longitude = %f",userCoordinate.longitude);

    self.mapView.delegate=self;
    NSMutableArray* annotations=[[NSMutableArray alloc] init];

    NSMutableArray *title = [self.itemss valueForKey:@"title"];
    NSMutableArray *subtitle = [self.itemss valueForKey:@"subtitle"];
    NSMutableArray *latitude = [self.itemss valueForKey:@"Lati"];
    NSMutableArray *longitude = [self.itemss valueForKey:@"Long"];
    NSMutableArray *gold = [self.itemss valueForKey:@"clubPurchased"];


    NSLog(@"%@", latitude);
    NSLog(@"%@", longitude);

    for (int i = 0; i < [title count]; i++){
        NSLog(@"%lu", (unsigned long)title.count);

        double lat = [[latitude objectAtIndex:i] doubleValue];
        double lng = [[longitude objectAtIndex:i] doubleValue];
        NSString *T = [title objectAtIndex:i];
        NSString *TT = [subtitle objectAtIndex:i];

         mine = [[gold objectAtIndex:i] boolValue];
        NSLog(@"BOOL:%hhd", mine);



        CLLocation *towerLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lng];
        [annotations addObject:towerLocation];
        CLLocationCoordinate2D coord = [[annotations lastObject] coordinate];

        PlacePin * myAnnotation1=[[PlacePin alloc] init];

        myAnnotation1.coordinate=coord;
        myAnnotation1.title=T;
        myAnnotation1.subtitle=TT;

        [self.mapView addAnnotation:myAnnotation1];
        [SVProgressHUD dismiss];

        }



}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    NSLog(@"welcome into the map view annotation");

    // if it's the user location, just return nil.


    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    // try to dequeue an existing pin view first
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKAnnotationView* pinView = [[MKAnnotationView alloc]
                                    initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];



    if (mine){
        pinView.image = [UIImage imageNamed:@"Iconn"];
       NSLog(@"TRUE");


    } else {
       NSLog(@"FALSE");

        pinView.image = [UIImage imageNamed:@"Iconn_gold"];


    }
    pinView.canShowCallout=YES;



    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];
    [rightButton addTarget:self
                    action:@selector(showDetails:)
          forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = rightButton;


    return pinView;
}
Milan1111
  • 185
  • 3
  • 13

1 Answers1

1

In -(void)Map, the mine value was changed in the for-loop and it registered a value of the last item in the loop (which is most probably false).

In viewForAnnotation, mine value wasn't changing for each annotation, thus you get all false when the annotations are displayed.

You will need to save mine value to your custom annotation PlacePin in -(void)Map for each item (just like title and subtitle) and retrieve the value in viewForAnnotation for the if-else statement.

Also, you may not be accessing your custom annotations correctly in viewForAnnotation, you can refer to this post.

Edit 2:

I believe PlacePin is your custom annotation. In PlacePin.h file,

@interface PlacePin : NSObject <MKAnnotation>

@property(nonatomic, assign) CLLocationCoordinate2D coordinate;
@property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) NSString *subtitle;
// Add this line
@property (strong, nonatomic) NSNumber *mine;

In - (void)Map,

PlacePin * myAnnotation1=[[PlacePin alloc] init];

myAnnotation1.coordinate=coord;
myAnnotation1.title=T;
myAnnotation1.subtitle=TT;
// Save the boolean value here
myAnnotation1.mine=[gold objectAtIndex:i];

In viewForAnnotation, try this:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    if ([annotation isKindOfClass:[PlacePin class]])
    {
        PlacePin *placePin = (PlacePin *)annotation;
        MKAnnotationView* pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"AnnotationIdentifier"];

        if(pinView)
        {
            pinView.annotation = annotation;
        }
        else
        {
            // Edit 2: Note this line!!
            pinView = placePin.annotationView;
        }
        // Use this to check instead
        if ([pinView.mine boolValue])
        {
            pinView.image = [UIImage imageNamed:@"Iconn"];
            NSLog(@"TRUE");
        } 
        else 
        {
            NSLog(@"FALSE");
            pinView.image = [UIImage imageNamed:@"Iconn_gold"];
        }
        ...
    }
}    
Community
  • 1
  • 1
Rick
  • 1,818
  • 1
  • 15
  • 18
  • I remade it by the post you suggested but by saving mine value you mean to save with NSUserDefaults? Because I have no idea how to save it. – Milan1111 Nov 08 '14 at 19:30
  • See my edit. (I am typing this outside XCode, let me know if there is error.) – Rick Nov 09 '14 at 16:03
  • The if/else in viewForAnnotation is unable to find me mine property(`if ([pinView.mine boolValue])`) – Milan1111 Nov 09 '14 at 17:10
  • adding `PlacePin *placepin = (PlacePin *)annotation;` fixed my problem, thanks! – Milan1111 Nov 09 '14 at 17:25
  • Glad that you figured out that line, I forgot about that. Do amend the second line that I added too. – Rick Nov 10 '14 at 13:56