MKMapView 和 addOverlay - 从 kml 解析覆盖

发布于 2024-10-21 00:47:53 字数 1726 浏览 1 评论 0原文

我正在尝试为一个简单的 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;
}

I am attempting to make a map overlay for a simple iPhone app. The problem is that even though the app complies with no error the polyline does not show up on the map. The console says that [overlay lastObject] is in fact a MKPolyline. Can someone maybe see what i am doing wrong here... I am new to iPhone app development?

Here is my relevant code for my mapView Controller:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活雷疯 2024-10-28 00:47:53

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

mapView.delegate = self;

Looks like the map view's delegate is not set in which case the viewForOverlay method will never get called. After the MKMapView alloc+initWithFrame line in viewDidLoad, add this:

mapView.delegate = self;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文