Search code examples
iosobjective-cmkmapviewmapkitmkannotationview

show multiple annotation in map view


i am new to map view in ios sdk. i want to show multiple annotation in map view using lat and long.basically all lat and long are coming from server side in json format. i am parsing all lat and long and saving it in different array. but how to show all annotation at single time. i am able to show only one annotation at a time.Below is code for single annotation i am using,

zoomLocation.latitude = latmpa.doubleValue;
zoomLocation.longitude = logmpa.doubleValue;
annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = zoomLocation;
annotationPoint.title = @"masjid....";
[mapView selectAnnotation:annotationPoint animated:YES];


[mapView addAnnotation:annotationPoint];

mapView.centerCoordinate = annotationPoint.coordinate;

MKCoordinateSpan span;
span.latitudeDelta = 1.5;
span.longitudeDelta = 1.0;
MKCoordinateRegion newRegion;
newRegion.center = zoomLocation;
newRegion.span = span;
[mapView setRegion:newRegion animated:YES];

Solution

  • Try This

    for ( int i=0; i<[yourLatLongarray count]; i++)
    {
    CLLocationCoordinate2D coord;
    
        coord.latitude=[[NSString stringWithFormat:@"%@",[yourLatitudeArray objectAtIndex:i]] floatValue];
        coord.longitude=[[NSString stringWithFormat:@"%@",
                          [yourLongitudeArray objectAtIndex:i]] floatValue];
        MKCoordinateRegion region1;
        region1.center=coord;
        region1.span.longitudeDelta=20 ;
        region1.span.latitudeDelta=20;
        [mapview setRegion:region1 animated:YES];
    
        NSString *titleStr =[namesArr objectAtIndex:i] ;
       // NSLog(@"title is:%@",titleStr);
    
      MyAnnotation*  annotObj =[[MyAnnotation alloc]initWithCoordinate:coord title:titleStr];
        [mapview addAnnotation:annotObj];
    
    }
    

    MyAnnotation.h is

    @interface MyAnnotation : NSObject <MKAnnotation>
    {   
       CLLocationCoordinate2D coordinate;
       NSString *title;
       NSString *subTitle;
       NSString *time;
    }
    
    @property (nonatomic)CLLocationCoordinate2D coordinate;
    
    @property (nonatomic, retain) NSString *title;
    
    @property (nonatomic, retain) NSString *subTitle;
    
    @property (nonatomic,retain) NSString *time;
    
    -(id)initWithCoordinate:(CLLocationCoordinate2D) c  title:(NSString *) t  subTitle:(NSString *)timed time:(NSString *)tim;
    
    -(id)initWithCoordinate:(CLLocationCoordinate2D) c title:(NSString *)tit;
    
    @end
    

    MyAnnotation.m is

    @implementation MyAnnotation
    
    @synthesize coordinate;
    
    @synthesize title;
    
    @synthesize time;
    
    @synthesize subTitle;
    
    -(id)initWithCoordinate:(CLLocationCoordinate2D) c  title:(NSString *) t  subTitle:(NSString *)timed time:(NSString *)tim
    {
       self.coordinate=c;
       self.time=tim;
       self.subTitle=timed;
       self.title=t;
       return self;
    }
    
    -(id)initWithCoordinate:(CLLocationCoordinate2D) c title:(NSString *)tit
    {
        self.coordinate=c;
        self.title=tit;
        return self;
    }
    
    @end