Any demo example Multiple Map Annotation using pli

2020-02-16 02:16发布

问题:

i want to use Map annotation i have plist list in which i have data and i want to show data on map using annotation. I have the demo example MapCallsout but i have to read plist on map?

回答1:

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;
}


回答2:

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);