0
我想使用地圖註釋我有plist列表,其中我有數據,我想 使用註釋在地圖上顯示數據。我有演示示例MapCallsout,但我有 在地圖上閱讀plist?任何演示示例多地圖註釋使用plist?
我想使用地圖註釋我有plist列表,其中我有數據,我想 使用註釋在地圖上顯示數據。我有演示示例MapCallsout,但我有 在地圖上閱讀plist?任何演示示例多地圖註釋使用plist?
首先獲取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;
}
下面是從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);