Remove a polyLine from the mapView
ios, mkmapview, polyline
Solution
In the code that loops through the map view's `overlays` array, this line is the problem:
if ([overlayToRemove isKindOfClass:[MKPolylineView class]])
The map view's `overlays` array contains objects of type `id<MKOverlay>` (the for-loop correctly declares `overlayToRemove` as such).
So the `overlays` array contains the model objects for the overlays and not the views.
The `MKPolylineView` class is the view for an `MKPolyline` overlay model.
So the `if` condition should be:
if ([overlayToRemove isKindOfClass:[MKPolyline class]])
Note that such a loop will remove all polylines from the map. If you wanted to delete specific polylines, you could set the `title` on each one when adding it and then check it before removing.
The second piece of code that checks and deletes `self.routeLine` directly should work as long as `self.routeLine` is not `nil` and contains a valid reference to an overlay currently on the map.
If you have only a single overlay on the map (the one polyline), you could also just call `removeOverlays` to delete all overlays from the map (whatever they are):
[self.mapView removeOverlays:self.mapView.overlays];
Problem
I've read many posts about it and still i have a problem. This is my code to draw a polyLine between two points: ``` -(void) drawAline:(CLLocation*)newLocation { //drawing a line CLLocationCoordinate2D coordinateArray[2]; coordinateArray[0] = CLLocationCoordinate2DMake(newLocation.coordinate.latitude, newLocation.coordinate.longitude); coordinateArray[1] = CLLocationCoordinate2DMake(self.jerusalem.coordinate.latitude, self.jerusalem.coordinate.longitude); self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:2]; [self.mapView setVisibleMapRect:[self.routeLine boundingMapRect]]; [self.mapView addOverlay:self.routeLine]; } -(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay { if(overlay == self.routeLine) { if(nil == self.routeLineView) { self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine]; self.routeLineView.fillColor = [UIColor blueColor]; self.routeLineView.strokeColor = [UIColor blueColor]; self.routeLineView.lineWidth = 5; } return self.routeLineView; } return nil; ``` } thats works fine. The problem is to remove the line. The next code doesn't work: ``` for (id<MKOverlay> overlayToRemove in self.mapView.overlays) { if ([overlayToRemove isKindOfClass:[MKPolylineView class]]) { [mapView removeOverlay:overlayToRemove]; } } ``` the next code doesn't work neither: ``` if (self.routeLine) { [self.mapView removeOverlay:self.routeLine]; self.routeLineView = nil; self.routeLine = nil; } ``` Thanks!