2011-03-18 84 views
1

我們試圖創建一個帶有從xml文件加載的註釋的mapview。這工作到目前爲止,並正在利用蘋果開發者庫上的KMLViewer代碼。現在我們試圖將XML文件中的數據加載到detailview中,但只加載相應的條目。因此,例如,當您點擊某個城市的詳細信息時,必須從該城市的xml文件中加載詳細信息。從地圖視圖(Iphone IOS)獲取DetailView中的xml數據

我們正在嘗試幾天,但不知道從哪裏開始。我們現在下面的代碼:

detailviewcontroller.m

#import "DetailViewController.h" 



@implementation DetailViewController 

@synthesize address; 




// Implement viewDidLoad to do additional setup after loading the view, typically from a nib 
- (void)viewDidLoad 
{ 
    TabbedCalculationAppDelegate *appDelegate = (TabbedCalculationAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    address.text = appDelegate.addressInput1 ; 

    [super viewDidLoad]; 
} 

- (void)viewDidUnload 
{ 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (void)dealloc 
{ 
    [super dealloc]; 
} 

地圖視圖

#import "locator.h" 
#import "DetailViewController.h" 

@implementation locator 

@synthesize map, detailViewController, rightButton, customPinView; 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // create a custom navigation bar button and set it to always says "Back" 
    UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init]; 
    temporaryBarButtonItem.title = @"Back"; 
    self.navigationItem.backBarButtonItem = temporaryBarButtonItem; 
    [temporaryBarButtonItem release]; 

    // Locate the path to the route.kml file in the application's bundle 
    // and parse it with the KMLParser. 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"branches" ofType:@"kml"]; 
    kml = [[KMLParser parseKMLAtPath:path] retain]; 

    // Add all of the MKOverlay objects parsed from the KML file to the map. 
    NSArray *overlays = [kml overlays]; 
    [map addOverlays:overlays]; 

    // Add all of the MKAnnotation objects parsed from the KML file to the map. 
    NSArray *annotations = [kml points]; 

    [map addAnnotations:annotations]; 

    // Walk the list of overlays and annotations and create a MKMapRect that 
    // bounds all of them and store it into flyTo. 
    MKMapRect flyTo = MKMapRectNull; 
    for (id <MKOverlay> overlay in overlays) { 
     if (MKMapRectIsNull(flyTo)) { 
      flyTo = [overlay boundingMapRect]; 
     } else { 
      flyTo = MKMapRectUnion(flyTo, [overlay boundingMapRect]); 
     } 
    } 

    for (id <MKAnnotation> annotation in annotations) { 
     MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate); 
     MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0); 
     if (MKMapRectIsNull(flyTo)) { 
      flyTo = pointRect; 
     } else { 
      flyTo = MKMapRectUnion(flyTo, pointRect); 
     } 
    } 

    // Position the map so that all overlays and annotations are visible on screen. 
    MKCoordinateRegion mapRegion; 
    mapRegion.center.latitude = 51.522416; 
    mapRegion.center.longitude = 5.141602; 
    mapRegion.span.latitudeDelta = 5; 
    mapRegion.span.longitudeDelta = 5; 
    [map setRegion:mapRegion animated:YES]; 
} 


#pragma mark MKMapViewDelegate 

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay 
{ 
    return [kml viewForOverlay:overlay]; 
} 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    // if it's the user location, just return nil. 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 

    // handle custom annotations 
    //  // try to dequeue an existing pin view first 
    static NSString* BridgeAnnotationIdentifier = @"bridgeAnnotationIdentifier"; 
    MKPinAnnotationView* pinView = (MKPinAnnotationView *) 
    [map dequeueReusableAnnotationViewWithIdentifier:BridgeAnnotationIdentifier]; 

    if (!pinView) 
    { 
     // if an existing pin view was not available, create one 
     customPinView = [[[MKPinAnnotationView alloc] 
               initWithAnnotation:annotation reuseIdentifier:BridgeAnnotationIdentifier] autorelease]; 
     customPinView.pinColor = MKPinAnnotationColorPurple; 
     customPinView.animatesDrop = YES; 
     customPinView.canShowCallout = YES; 

     // add a detail disclosure button to the callout which will open a new view controller page 
     // 
     // note: you can assign a specific call out accessory view, or as MKMapViewDelegate you can implement: 
     // - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control; 
     // 
     rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     customPinView.rightCalloutAccessoryView = rightButton; 

     return customPinView; 
    }else{ 
     return pinView;} 

    return nil; 

} 

#pragma mark - 
#pragma mark MKMapViewDelegate 

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 
    { 
     if (view.annotation == mapView.userLocation) 
      return; 

     rightButton = (DetailViewController *)view.annotation;   
     //show detail view using buttonDetail... 
    } 
    // the detail view does not want a toolbar so hide it 
    [self.navigationController setToolbarHidden:YES animated:YES]; 


    [self.navigationController pushViewController:self.detailViewController animated:YES]; 
} 

- (void)viewDidUnload 
{ 
    self.detailViewController = nil; 

} 

- (void)dealloc 
{ 
    [detailViewController release]; 
    [super dealloc]; 
} 

@end 

正如你可以看到代碼開始嘗試噸的東西后,顯得凌亂,但我們不真的不知道從哪裏開始。

任何幫助將非常讚賞

日Thnx提前!

回答

2

請查看KMLParser中KMLPlacemark的界面,您可以看到XML地標元素的正確解析和存儲。例如地址缺失。所以,你將不得不通過實現在KMLPlacemark類中的字段添加所有你想要的解析器來收集信息並改變KMLParser方法:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName ... 
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName ... 

還有KMLPlacemark實施的某些部分。要使用解析器填充新字段,您必須編寫諸如- (void)beginName- (void)endName的方法。當你想要解析的元素有孩子時,它會變得有點棘手。

將KMLParser文件分成幾個文件,每個文件包含一個類可能很有幫助。

如果您已經實現了這一功能,並且您的地標包含了所有必需的詳細信息,那麼您可以使用MKMapViewDelegate協議捕獲註解的輕敲。實施didDeselectAnnotationView,這可能是這樣的:

- (void) mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view 
{ 
    // implementation example of placemarkForAnnotation below 
    KMLPlacemark * placemark = [kml placemarkForAnnotation:view.annotation]; 

    MyDetailViewController * myDetailViewController = [[MyDetailViewController alloc] initWithPlacemark:placemark]; 

    [self presentModalViewController:myDetailViewController animated:YES]; 

    [myDetailViewController release]; 
} 

在KMLParser添加

- (KMLPlacemark *)placemarkForAnnotation:(id <MKAnnotation>)point 
{ 
    // Find the KMLPlacemark object that owns this point and return it 
    for (KMLPlacemark *placemark in _placemarks) { 
     if ([placemark point] == point) 
      return placemark; 
    } 
    return nil; 
} 

希望我能爲你指明正確的方向。這將是一些工作;)

+0

thnx爲深入的解釋,當然可以使用這個! – IosQuestions 2011-03-25 13:39:35