简体   繁体   中英

MKPolyline not being displayed in map view

I am working on a simple iOS app that retrieves GeoJSON from a server and displays it on a map view. Everything is working except the last part. I retrieve the GeoJSON, parse it to create a MKPolyline, give the line to my view (which is the map's delegate), and then I add the line to my mapView, but it doesn't show. I am out of ideas on what can be going wrong.

I have a networking class that retrieves data from the server, and my view controller is its delegate. It receives the line like this:

-(void) receiveRoutePolyline:(MKPolyline *)routeLine {
    NSLog(@"Received line");
    NSLog(@"%@", self.mapView.delegate);
    NSLog(@"%@", self);
    self.routeLine = routeLine;
    if (self.routeLine == nil) {
        NSLog(@"Nil duh!");
    }
    NSLog(@"%lu", (unsigned long)[self.routeLine pointCount]);
    [self.mapView addOverlay:self.routeLine level:MKOverlayLevelAboveLabels];
    NSLog(@"Set line on map");
}

As you can see I've been printing information to find any mistakes. Here is the output of the function:

2016-04-28 12:09:40.472 SinTraficoRouteAPIDemo[43189:1400965] Received line
2016-04-28 12:09:40.473 SinTraficoRouteAPIDemo[43189:1400965] <MapViewController: 0x78f37a90>
2016-04-28 12:09:40.474 SinTraficoRouteAPIDemo[43189:1400965] <MapViewController: 0x78f37a90>
2016-04-28 12:09:40.475 SinTraficoRouteAPIDemo[43189:1400965] 121
2016-04-28 12:09:40.475 SinTraficoRouteAPIDemo[43189:1400965] Set line on map

The view controller is properly set as the mapView's delegate, however, the method for rendering:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
    NSLog(@"hi");
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolyline *route = overlay;
        MKPolylineRenderer *routeRenderer = [[MKPolylineRenderer alloc] initWithPolyline:route];
        routeRenderer.strokeColor = [UIColor blueColor];
        routeRenderer.lineWidth = 10;
        return routeRenderer;
    }
    else {
        return nil;
    }
}

never logs "hi" to the console! I even tried adding the deprecated method

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay {
    MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
    polylineView.strokeColor = [UIColor redColor];
    polylineView.lineWidth = 10;
    return polylineView;
}

to see if maybe it would work, but no luck. Any help is appreciated, thank you.

EDIT: View controller declaration

@interface MapViewController : UIViewController <MKMapViewDelegate, OptionsDelegate, RouteNetworkDelegate>

This code was working fine. The line was being painted on the map correctly, the problem was the code that parsed the coordinates and created the line. It was setting the latitude and longitude backwards so the line was somewhere else in the world, invisible to my level of zoom.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM