简体   繁体   中英

Mkpolyline is not being drawn

The route is clearly being made but the polyline is not drawn. I am able to find the total distance, and have verified that the coordinates we are using are not (0,0). Is there something wrong with the delegate, since it seems that both the addOverlay and addAnnotation (called in a custom method shown below called createAndAddAnnotationForCoordinate) methods are not working?

-(void)viewDidLoad {

[super viewDidLoad];
// Do any additional setup after loading the view.

//generates map view
mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 
self.view.bounds.size.height)];
//setting delegate to self is not fixing the error
mapView.delegate = self;


mapView.mapType = MKMapTypeStandard;




//converting CLLocationCoordinate2D to MKPlacemark
MKPlacemark *startPlacemark = [[MKPlacemark alloc]
initWithCoordinate: addressOneCoords addressDictionary:

[NSDictionary dictionaryWithObjectsAndKeys:nil]];
MKPlacemark *endPlacemark = [[MKPlacemark alloc]initWithCoordinate: addressTwoCoords addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:nil]];

//converting MKPlacemark to MKMapItem
MKMapItem *start = [[MKMapItem alloc ]initWithPlacemark:startPlacemark];

MKMapItem *end = [[MKMapItem alloc]initWithPlacemark:endPlacemark];


MKDirectionsRequest *request = [MKDirectionsRequest new];
[request setSource:start];
[request setDestination:end];
[request setTransportType:MKDirectionsTransportTypeAutomobile];
request.requestsAlternateRoutes = YES;

//Just to check if the coordinates were transferred successfully between view controllers and they did transfer successfully
NSLog(@"address one lat is %f",addressOneCoords.latitude);
NSLog(@"address one lon is %f",addressOneCoords.longitude);
NSLog(@"address two lat is %f",addressTwoCoords.latitude);
NSLog(@"address two lon is %f",addressTwoCoords.longitude);

MKDirections *directions = [[MKDirections alloc]initWithRequest:request];

[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse response, NSError error){
    //if the route can't be created
    if(error){
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Unable to create route" message:@"Go back to check if addresses are valid" delegate:nil cancelButtonTitle:@"Ok"otherButtonTitles:nil];
        [alert show];
    }
    else{

        [mapView removeOverlays:self.mapView.overlays];

        MKRoute *mainRoute = [response.routes firstObject];



        routeLine = mainRoute.polyline;


        if(routeLine){
            [self.mapView removeOverlay:routeLine];
        }

        //the addOverlay method is not drawing the polyline
        [self.mapView addOverlay: routeLine level:MKOverlayLevelAboveRoads];

        //proof that route is being created successfully
        NSLog(@"Total distance is %f", mainRoute.distance);

        MKMapPoint middlePoint = mainRoute.polyline.points[mainRoute.polyline.pointCount/2];

        //also, the addannotation method is not being called either it seems like
        [self createAndAddAnnotationForCoordinate:MKCoordinateForMapPoint(middlePoint)];


    }
}];


}

Our createAndAddAnnotationForCoordinate method,

 -(void) createAndAddAnnotationForCoordinate : (CLLocationCoordinate2D)coordinate{

MKPointAnnotation* annotation = [[MKPointAnnotation alloc]init];
annotation.coordinate = coordinate;
annotation.title = @"Point";
annotation.subtitle = @"subtitle";
[mapView addAnnotation:annotation];


}

Our overridden mapviewdelegate method,

-(MKOverlayRenderer )mapView:(MKMapView )mapView rendererForOverlay:(id<MKOverlay>)overlay{

if([overlay isKindOfClass:[MKPolyline class]]){
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc]initWithOverlay:overlay];
renderer.strokeColor = [UIColor redColor];
renderer.lineWidth = 10.0f;

return renderer;
}

else
    return nil;
}

The output if address one was somewhere in NJ and address two was somewhere in CA:

address one lat is 40.902599
address one lon is -74.407097
address two lat is 34.054435
address two lon is -118.253393
Total distance is 4459771.000000

Ron, I'm suspecting your polyline ( mainRoute.polyline ) - your rendererForOverlay looks almost exactly like one I am using successfully. Barring the very basic mistakes like not setting the MKMapView delegate, or setting it to some other object, I would be almost sure the polyline you add to the map does not contain valid CLLocationCoordinate2D structs.

In Swift, creation goes like

    var coordinatePtr = UnsafeMutablePointer<CLLocationCoordinate2D>(track2DCoordinates)
    let trackPolygon = MKPolyline(coordinates: coordinatePtr, count: track2DCoordinates.count)
    mapView.removeOverlays(mapView.overlays)
    mapView.addOverlay(trackPolygon)

Start by verifying that you really have a valid MKPolyline.

I'm also not sure about your middlePoint calculation.

MKMapPoint middlePoint = mainRoute.polyline.points[mainRoute.polyline.pointCount/2];

This kind of thing probably works right now but in Swift you need to be a lot more careful of the data types used as index. What if you have an odd number of points, or zero?

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