【问题标题】:MKMapView and addOverlay - parsing overlay from kmlMKMapView 和 addOverlay - 从 kml 解析覆盖
【发布时间】:2012-01-29 06:41:08
【问题描述】:

我正在尝试为一个简单的 iPhone 应用程序制作地图叠加层。问题是即使应用程序没有错误,折线也不会显示在地图上。控制台说 [overlay lastObject] 实际上是一个 MKPolyline。有人可能会看到我在这里做错了什么...我是 iPhone 应用程序开发的新手吗?

这是我的 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;
}

【问题讨论】:

    标签: iphone objective-c mkmapview overlay


    【解决方案1】:

    看起来地图视图的委托没有设置,在这种情况下 viewForOverlay 方法将永远不会被调用。在 viewDidLoad 中的 MKMapView alloc+initWithFrame 行之后,添加:

    mapView.delegate = self;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多