0

I am adding MKPolyline using an array of lat long values. When I draw annotation over the polyline then Annotation view goes above Trees,flyovers and MKPolyline goes below these objects. Is there any way to solve this problem? What I am trying is below:

    // access location array to get lat long values

            NSInteger numberOfSteps = locations.count;
            CLLocationCoordinate2D coordinates[numberOfSteps];
            for (NSInteger index = 0; index < numberOfSteps; index++) {
                CLLocation *location = [locations objectAtIndex:index];
                CLLocationCoordinate2D coordinate = location.coordinate;
                coordinates[index] = coordinate;
            }

         MKPolyline *polyLine = [MKPolyline     polylineWithCoordinates:coordinates count:numberOfSteps];
            [_routeMapView addOverlay:polyLine level:MKOverlayLevelAboveRoads];

//Here I am adding custom annotation pin.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{

    if (![annotation isKindOfClass:[CustomPointAnnotation class]])
        return nil;

    NSString *reuseId = @"test";

    MKAnnotationView *anView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
    if (anView == nil) {
        anView = [[MKAnnotationView alloc] initWithAnnotation:point reuseIdentifier:reuseId];
        anView.canShowCallout = NO;
    }
    else
    {
        anView.annotation = annotation;
    }

    //Set annotation-specific properties **AFTER**
    //the view is dequeued or created...

    CustomPointAnnotation *cpa = (CustomPointAnnotation *)annotation;
    anView.image = [UIImage imageNamed:cpa.imageName];
    return anView;
}

// show route from source to destination

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolyline class]])
    {
        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
        renderer.strokeColor = [[UIColor redColor] colorWithAlphaComponent:0.7];
        renderer.lineWidth   = 4;
        return renderer;
    }
    return nil;
}

Here is what I am having issue :

enter image description here

VIVEK
  • 111
  • 2
  • 6

1 Answers1

0

This is probably a duplicate from this: How do I show MKOverlay above MKAnnotations?

As suggested there since iOS 7.0+, when you add your MKOverlay to the map, use the addOverlay:level: method. This allows you to specify the z-position of the overlay. One of the highest z-positions for the overlay as defined by MKOverlayLevels is MKOverlayLevelAboveLabels:

Place the overlay above map labels, shields, or point-of-interest icons but below annotations and 3D projections of buildings.

pesch
  • 1,976
  • 2
  • 17
  • 29
  • Hi @pesch , thanks for reply but the above link is doing reverse of what I want. Actually annotation pin is showing correctly above the polyline but at some places where polyline passes below Trees,flyovers then I want annotation pin should also pass below these . – VIVEK May 23 '17 at 10:56
  • From what I understand, the vertical level for your `MKPolyline` is not OK. Why don't you try changing it's level? – pesch May 23 '17 at 11:01
  • you mean to change its level to MKOverlayLevelAboveLabels ?. But this is not helping either because polyline still goes under Trees and bridges. – VIVEK May 23 '17 at 11:51