MKMapView 和 addOverlay - 从 kml 解析覆盖
我正在尝试为一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来地图视图的委托未设置,在这种情况下 viewForOverlay 方法将永远不会被调用。在 viewDidLoad 中的 MKMapView alloc+initWithFrame 行之后,添加以下内容:
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: