Any demo example Multiple Map Annotation using plist?

2 Solutions Collect From Internet About “Any demo example Multiple Map Annotation using plist?”

First get All The Locations from Plist and put them into an Array.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *fooPath = [documentsPath stringByAppendingPathComponent:@"yourPlistFile.plist"];
NSLog(fooPath);
NSArray *contentArray = [NSArray arrayWithContentsOfFile:fooPath];
NSLog(@"%@",contentArray);

Use Below code for showing multiple annotations on your map.

NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:10];
        for (id annotation in yourMapView.annotations)
            if (annotation != yourMapView.userLocation)
                [toRemove addObject:annotation];
        [yourMapView removeAnnotations:toRemove];


        yourMapView.delegate = self;

        [yourMapView setMapType:MKMapTypeStandard];

        MKCoordinateRegion region;

        MKCoordinateSpan span;
        span.latitudeDelta=0.2;  
        span.longitudeDelta=0.2; 


        LocationClass *locationData=[[LocationClass alloc]init];



        for (int k=0; k<contentArray.count; k++) {

            locationData=[contentArray objectAtIndex:k];

            CLLocationCoordinate2D location; 

            region.span = span;
            region.center = location;


            location.latitude =[locationData.latitude doubleValue];
            location.longitude = [locationData.longitude doubleValue];

            AnnView *mapPoint = [[AnnView alloc] initWithLocation:location];

            mapPoint.title=[NSString stringWithFormat:@"%@ @",locationData.Title];
            mapPoint.subtitle=[NSString stringWithFormat:@"%@ ",locationData.Subtitle];

            [yourMapView addAnnotation:mapPoint];

            mapPoint = nil;


            [yourMapView setRegion:region animated:YES];
            [yourMapView regionThatFits:region];


        }

        [self zoomToFitMapAnnotations:yourMapView]; 

Then Write the delegate Method Like Below Code.

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


    MKAnnotationView * annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"annot"];
    if (!annotationView) {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annot"] ;
        annotationView.canShowCallout = YES;
    }
    else {
        annotationView.annotation = annotation;
    }

        annotationView.image = [UIImage imageNamed:@"pinimage.png"];

    return annotationView;
}

Here’s the sample for retrieving data from plist file, the data will store into NSArray. You can show annotations using this array.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *fooPath = [documentsPath stringByAppendingPathComponent:@"yourPlistFile.plist"];
NSLog(fooPath);
NSArray *contentArray = [NSArray arrayWithContentsOfFile:fooPath];
NSLog(@"%@",contentArray);