2013-02-22 27 views
1

我正在構建一個具有MKMapview的應用程序。我的代碼所在的視圖擴展了MKMapView。我的KML數據中的多邊形沒有顯示在我的IOS 6地圖應用程序中

我得到數據後,我創建了一個KMLParser,並讓它解析我的KML文件,看起來工作得很好。我通過蘋果開發者文檔中的示例獲得了我的KMLParser的代碼http://developer.apple.com/library/ios/#samplecode/KMLViewer/Introduction/Intro.html

就像我說的代碼似乎工作。我的一些KML文件只將標記加載到地圖上,它們工作得很好。這是需要繪製多邊形疊加層的KML文件,這些疊加層看起來不像是渲染。我想知道底部附近的MKMapViewDelegate代碼是不是正在做它需要做的事情?我在那裏放置了斷點,並且它們似乎從未被擊中。

這是我的看法.h和.m的樣子。

.H

#import <MapKit/MapKit.h> 
#import "MainDM.h" 
#import "BBView.h" 
#import "MapPM.h" 
#import "KMLParser.h" 

@class MapPM; 
@class MainDM; 

@interface MapView : MKMapView <BBView, MKMapViewDelegate> 
{ 
    NSArray *overlays; 
    KMLParser *parser; 
} 

@property(nonatomic,strong)MainDM* dm; 
@property(nonatomic,strong)MapPM* pm; 
-(void)update:(DataObject*)data; 
-(id)initWithModel:(MainDM*)dm:(CGRect)frame; 
@end 

的.m

#import "MapView.h" 

@implementation MapView 

-(id)initWithModel:(MainDM*)dm:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) 
    { 
     _dm = dm; 
     [_dm register:self]; 
     _pm = [[MapPM alloc] initWithModel:_dm]; 

     CLLocationCoordinate2D coord = {.latitude = 32.61724, .longitude = -106.74128}; 
     MKCoordinateSpan span = {.latitudeDelta = 1, .longitudeDelta = 1}; 
     MKCoordinateRegion region = {coord, span}; 

     [self setRegion:region]; 
    } 
    return self; 
} 

-(void)update:(DataObject*)data 
{ 
    if([[data getType] isEqualToString:@"CURRENT_KML_CHANGE"]) 
    { 
     KmlVO *d = (KmlVO*)[data getData]; 
     parser = [_pm showMKL:d]; 

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

     // Add all of the MKAnnotation objects parsed from the KML file to the map. 
     NSArray *annotations = [parser points]; 
     [self 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 o) 
     { 
      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. 
     //self.visibleMapRect = flyTo; 

    } 
} 


#pragma mark MKMapViewDelegate 

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay 
{ 

    return [parser viewForOverlay:overlay]; 
} 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 

    return [parser viewForAnnotation:annotation]; 
} 

@end 

回答

0

我想通了,我沒有告訴視圖是代表這樣在底部繪製多邊形層的代碼未運行。

我只是增加了以下我initWithModel功能

self.delegate = self; 
相關問題