2011-03-09 104 views
2

我正在嘗試爲簡單的iPhone應用程序製作地圖覆蓋圖。問題是,即使應用程序沒有符合任何錯誤,折線也不會顯示在地圖上。控制檯說[overlay lastObject]實際上是一個MKPolyline。有人可能會看到我在這裏做錯了什麼...我是iPhone應用程序開發新手?MKMapView和addOverlay - 從kml解析覆蓋圖

這是我爲我的MapView控制器相關代碼:

- (void)viewDidLoad { 
    [super viewDidLoad];  

    CGRect mapFrame = CGRectMake(0.0f, 31.0f, 320.0f, 370.0f); 
    mapView = [[MKMapView alloc] initWithFrame:mapFrame]; 

    MKCoordinateRegion region; 
    MKCoordinateSpan span; 
     span.latitudeDelta=.02; 
     span.longitudeDelta=.02; 

    CLLocationCoordinate2D location; 
     location.latitude = 29.43421; 
     location.longitude = -98.48436; 

     region.span=span; 
     region.center=location; 

    NSURL *url = [NSURL URLWithString:@"They asked me not to post this... It is a valid KML file though"]; 
    kml = [[KMLParser parseKMLAtURL:url] retain]; 

    // Add all of the MKOverlay objects parsed from the KML file to the map. 
    NSArray *overlay = [kml overlays];  
    NSLog(@"TEST: %@",[overlay lastObject]); 

    [mapView setRegion:region animated:TRUE]; 
    [mapView regionThatFits:region];   
    [mapView addOverlay:[overlay lastObject]]; 

    [self.view insertSubview:mapView atIndex:0];  

    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bkgd.png"]]; 
    self.view.backgroundColor = background; 
    [background release]; 
    [url release]; 

} 

#pragma mark- 
#pragma mark MKMapViewDelegate 
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay 
{  
    MKPolylineView *line = [[[MKPolylineView alloc] initWithPolyline:overlay] autorelease]; 
    line.strokeColor = [UIColor blueColor]; 
    line.lineWidth = 5; 
    return line; 
} 

回答

12

看起來像地圖視圖的委託未設置在這種情況下viewForOverlay方法永遠不會被調用。在viewDidLoad中的MKMapView alloc + initWithFrame行後面加上:

mapView.delegate = self; 
+0

謝謝!!!很棒。 – Katherine 2011-03-09 22:50:36