2
votes

I have a mapview that displays locations of cash points. Annotations are dropped and the callout can be clicked on to go to a page with more detail about that location. There are two categories of cashpoint, free and paid, free cashpoint pins are green and the other red. When the pins drop they are the correct colours. Everything works fine untill i zoom to user location or other areas of the map then when i go back to the pins they have lost their colour formatting and are all the original red colour.

I assume this is something to do with the map reloading when it downloads tiles and not reloading the pins properly, although i could be wrong.

Any help will be much appreciated.

here is my code:

#import "CashPointMapViewController.h"
#import "PinDrop.h"
#import "CashPointDetailViewController.h"

@implementation CashPointMapViewController
@synthesize app, theCashList, mapview, ann, count, myArray, pinColor;

- (IBAction) getlocation { 

    MKCoordinateRegion region;
    region.center = self.mapview.userLocation.coordinate;
    region.span.longitudeDelta = 0.01f;
    region.span.longitudeDelta = 0.01f;
    [mapview setRegion:region animated:YES];
}

- (void)viewDidLoad {

    [super viewDidLoad];

    mapview.showsUserLocation = YES;

    [mapview setMapType:MKMapTypeStandard];
    [mapview setZoomEnabled:YES];
    [mapview setScrollEnabled:YES];

    MKCoordinateRegion region = { {0.0, 0.0 }, {0.0, 0.0 } };
    region.center.latitude = 53.801279;
    region.center.longitude = -1.548567;
    region.span.longitudeDelta = 0.3f;
    region.span.longitudeDelta = 0.3f;
    [mapview setRegion:region animated:YES];

    app = [[UIApplication sharedApplication]delegate];

    UIImage *locate = [UIImage imageNamed:@"location arrow white.png"];

    UIBarButtonItem *userlocatebutton = [[UIBarButtonItem alloc] initWithImage:locate     style:UIBarButtonItemStylePlain target:self action:@selector(getlocation)];

    self.navigationItem.rightBarButtonItem = userlocatebutton;

    [self performSelectorInBackground:@selector(annloop) withObject:self];

}


-(void) annloop {

    int i;
    for (i=0; i<=count-1; i = i+1) {
    theCashList = [myArray objectAtIndex:i];

    NSString *trimlat = [theCashList.lat stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *trimlon = [theCashList.lon stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    double latdouble = [trimlat doubleValue];
    double londouble = [trimlon doubleValue];

    CLLocationCoordinate2D coord = {(latdouble),(londouble)};

    ann = [[PinDrop alloc] init];

    ann.index = i;

    ann.title = [theCashList.name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    NSString *street = [theCashList.street stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *town = [theCashList.town stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *postcode = [theCashList.postcode stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    NSString *address = [[NSString alloc] initWithFormat:@"%@, %@, %@", street, town, postcode];

    ann.subtitle = address;
    ann.coordinate = coord;

    NSString *trimprice = [theCashList.price stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    if ([trimprice isEqualToString:@"Free"])
    {
        ann.price = 1;
    }
    else
    {
        ann.price = 0;
    }
    [mapview performSelectorOnMainThread:@selector(addAnnotation:) withObject:ann waitUntilDone:YES];
    }
}


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

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

    MKPinAnnotationView *mypin = [[MKPinAnnotationView alloc]initWithAnnotation:ann reuseIdentifier:@"current"];
    mypin.backgroundColor = [UIColor clearColor];
    UIButton *goToDetail = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    mypin.rightCalloutAccessoryView = goToDetail;
    mypin.draggable = NO;
    mypin.animatesDrop = TRUE;
    mypin.canShowCallout = YES;


    if (ann.price == 1)
    {
        mypin.pinColor = MKPinAnnotationColorGreen;
    }
    else
    {
        mypin.pinColor = MKPinAnnotationColorRed;
    }
    return mypin;
    }


- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control {

   PinDrop *annView = view.annotation;
    CashPointDetailViewController *detailView = [[CashPointDetailViewController alloc]init];
    theCashList = [myArray objectAtIndex:annView.index];
    detailView.theCashList = theCashList;
    [self.navigationController pushViewController:detailView animated:YES];

}


- (void)viewDidUnload {

    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:   (UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

EDIT: Here is my .h if it helps.

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "CashPointList.h"
#import <MapKit/MapKit.h>
#import "PinDrop.h"

@interface CashPointMapViewController : UIViewController  {

    MKMapView *mapview;
    PinDrop *ann;
}

    @property (nonatomic, retain) AppDelegate *app;
    @property (nonatomic, retain) CashPointList *theCashList;
    @property (nonatomic, retain) PinDrop *ann;
    @property (nonatomic, retain) IBOutlet MKMapView *mapview;
    @property (nonatomic, readwrite) int count;
    @property (nonatomic, retain) NSMutableArray *myArray;
    @property (nonatomic) MKPinAnnotationColor pinColor;

    -(IBAction) getlocation;


@end
1

1 Answers

3
votes

It's not a problem related to the map reloading tiles.

The issue is that the code in the viewForAnnotation delegate is using the ann object with the incorrect assumption that the class-instance-level ann object will be in sync with whenever the delegate method is called.

The viewForAnnotation delegate is not necessarily called in the order that you add annotations and can be called multiple times for the same annotation if the map needs to re-display an annotation when it comes back into view.

When the delegate method gets called again for a previously added annotation, ann and annotation no longer point to the same object. ann is now probably pointing to the last added annotation and so all the annotations change to its color.

In that delegate method, you must use the annotation parameter which is a reference to the annotation that the map view wants the view for in the current call (which may be completely unrelated to your outside loop).

So this line:

MKPinAnnotationView *mypin = [[MKPinAnnotationView alloc] 
    initWithAnnotation:ann reuseIdentifier:@"current"];

should be:

MKPinAnnotationView *mypin = [[MKPinAnnotationView alloc] 
    initWithAnnotation:annotation reuseIdentifier:@"current"];
                       ^^^^^^^^^^

and when checking the annotation's properties, use the annotation parameter (and cast it to your custom class to get at the custom properties):

PinDrop *ann = (PinDrop *)annotation;
//Note that this local "ann" is NOT the same as the class-level "ann".
//May want to use a different name to avoid confusion.
//The compiler may also warn you about this.

if (ann.price == 1)
...


A separate, unrelated, but highly recommended suggestion is to implement annotation view re-use by using dequeueReusableAnnotationViewWithIdentifier. This will improve performance when you have lots of annotations.