0

Well, I have a view with a map where I load some annotations. Everything works fine, but when I want to erase those annotations and include other annotations they do not show up until I move (scroll) the map.

Any suggestion?

EDITED:

Seems like instead of using this:

[self updateMap];

I have to use:

[self performSelectorOnMainThread:@selector(updateMap) withObject:nil waitUntilDone:true];

Being update map the method where I add my new annotations.

sergiocg90
  • 489
  • 2
  • 11
  • 24

2 Answers2

0

Try smth like this:

[mapView removeAnnotations:[mapView annotations]];
NSArray *targetArray = /*YOUR ARRAY NAME with object, containing `latitude`, `longitude` and `title` values*/
for (id *item in targetArray) {
    if (item.latitude && item.longitude) {
        MKPin *pin = [[MKPin alloc] init]; // MKPin is your class which specifies the deledate `MKAnnotation`
        pin.coordinate = CLLocationCoordinate2DMake(item.latitude, item.longitude);
        pin.title = item.title;
        [mapView addAnnotation:pin];
    }
}
demon9733
  • 1,054
  • 3
  • 13
  • 35
  • No, I am using a default view for each annotation and I need the `mapView: viewForAnnotation:` method to be called, but it is not called until I scroll the map – sergiocg90 Apr 19 '12 at 11:43
  • If you using default view for annotation, you have not to implement `mapView:viewForAnnotation:` method. Also, if you need, you can run this method manually. In the condition block in the end write this: `[self mapView:mapView viewForAnnotation:pin];` – demon9733 Apr 19 '12 at 11:55
0

In the viewdidload use :

- (void)viewDidLoad 

{

    NSMutableArray* annotations=[[NSMutableArray alloc] init];
    MyAnnotation* myAnnotation = [[MyAnnotation alloc] init];
myAnnotation.coordinate = theCoordinate;
    [annotations addObject:myAnnotation];
    MKMapRect flyTo = MKMapRectNull;
for (id <MKAnnotation> annotation in annotations) 
   {
    NSLog(@"fly to on");
        MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
        MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
        if (MKMapRectIsNull(flyTo)) 
        {
               flyTo = pointRect;
        }
        else
        {
               flyTo = MKMapRectUnion(flyTo, pointRect);
       //NSLog(@"else-%@",annotationPoint.x);
        }
     }

// Position the map so that all overlays and annotations are visible on screen.
mapView.visibleMapRect = flyTo;

}

- (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";
MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
                                 initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
pinView.draggable = NO;
pinView.pinColor=MKPinAnnotationColorGreen;

//button on the right for popup for pins
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
                action:@selector(showDetails:)
      forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;

//zoom button on the left of popup for pins
UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
[leftButton setTitle:annotation.title forState:UIControlStateNormal];
[leftButton addTarget:self 
               action:@selector(zoomToLocation:) forControlEvents:UIControlEventTouchUpInside];
pinView.leftCalloutAccessoryView = leftButton;

UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"profile.png"]];
pinView.leftCalloutAccessoryView = profileIconView;
[profileIconView release];


return pinView;

}

Hopefully this helps !!!!!!!!!

taus-iDeveloper
  • 681
  • 2
  • 8
  • 22