2011-03-18 13 views
1

xmlファイルから読み込まれたアノテーションを使用してマップビューを作成しようとしています。これまでのところ、Appleの開発者用ライブラリで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

KMLPlasemarkのKMLPlacemarkのインターフェイスを見てください。そこには、xml目印の要素が正確に解析されて保存されているかどうかを確認できます。たとえば、アドレスがありません。

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

などKMLPlacemarkの実装のいくつかの部分:だからあなたはパーサがKMLPlacemarkクラスのフィールドを実装することにより収集し、KMLParser方法を変更したいすべての情報を追加する必要があります。新しいフィールドをパーサーで埋めるには、- (void)beginName- (void)endNameのようなメソッドを書く必要があります。解析したい要素に子要素がある場合は、少し難しいでしょう。

KMLParserファイルを1つのクラスを含む複数のファイルに分割すると便利です。

これが達成され、目印に必要な詳細がすべて含まれている場合は、MKMapViewDelegateプロトコルでアノテーションのタップをキャッチできます。このようになりますdidDeselectAnnotationViewを実装します。KMLParserで

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

私は正しい方向にあなたを指すことができ

- (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

関連する問題