0

我想使用地图注释我有 plist 列表,其中我有数据,我想使用注释在地图上显示数据。我有演示示例 MapCallsout,但我必须阅读地图上的 plist?

4

2 回答 2

1

这是从 plist 文件中检索数据的示例,数据将存储到NSArray. 您可以使用此数组显示注释。

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);
于 2012-08-28T09:03:22.740 回答
0

首先从 Plist 中获取所有位置并将它们放入一个数组中。

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

使用下面的代码在地图上显示多个注释。

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

然后像下面的代码一样编写委托方法。

- (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;
}
于 2012-08-28T09:34:21.450 回答