我想使用Map注释我有plist列表,其中我有数据,我想 使用注释在地图上显示数据。我有演示示例MapCallsout但我有 在地图上阅读plist?
答案 0 :(得分: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);
答案 1 :(得分: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;
}