2011-03-09 108 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