Im writing a program in IOS where i mark a few spots on the map with a pin.
Now i have the dequereusableannotationview code that works fine with ONE pin but crashes with i use two pins or try to mark two points on the map.
IT only works when i either comment out ONE annotation or comment out the dequeucode
But the question is….do i even need that dequeue code? as the most pins i will ever have is probably…10..?
Thanks you
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
MKMapView *mv=[[MKMapView alloc]initWithFrame:CGRectZero];
mv.delegate=self;
[self setView:mv];
MapAnnotation* ann=[[MapAnnotation alloc]init];
CLLocationCoordinate2D location;
location.latitude=(double)51.501468;
location.longitude=(double)-0.141596;
[ann setCoordinate:location];
[ann setTitle:@"test"];
[ann setTitle:@"plz work"];
[mv addAnnotation:ann];
[ann release];
MapAnnotation* AnnB=[[MapAnnotation alloc]init];
CLLocationCoordinate2D locationB;
locationB.latitude=(double)16.4944;
locationB.longitude=(double)-151.7364;
[AnnB setCoordinate:locationB];
[AnnB setTitle:@"test"];
[AnnB setSubtitle:@"work"];
[mv addAnnotation:AnnB];
[AnnB release];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKPinAnnotationView *pinView=nil;
if (annotation!=mapView.userLocation) {
static NSString *defaultID=@"MYLoction";
pinView=(MKPinAnnotationView *)[mapView
dequeueReusableAnnotationViewWithIdentifier:defaultID];
if (pinView=nil) {
pinView=[[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:defaultID]autorelease];
pinView.pinColor=MKPinAnnotationColorPurple;
pinView.canShowCallout=YES;
pinView.animatesDrop=YES;
}
}
}
One problem is that when you test for equality in
you are using the assignment operator,
=, instead of the equality operator,==. This will causenilto be assigned topinViewevery time, so you will never reuse an annotation. I’m surprised the compiler didn’t catch this.Also, you aren’t actually returning anything in
viewForAnnotation:. Try this instead: