MKMapView and addOverlay - parsing overlay from kml
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];
[mapVi开发者_如何学Cew 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;
}
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;
精彩评论