Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift remove map overlays

I am trying to remove overlays from map.

func removeMapOverlay() {
    
    var removeOverlays : [AnyObject]! = self.mapView.overlays
    // Above line throws runtime exception

    self.mapView.removeOverlays(removeOverlays)
}

self.mapView.overlays are type of AnyObject array. var overlays: [AnyObject]! { get }.

So initially I wrote

var removeOverlays = self.mapView.overlays

It throws EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) exception at this line on runtime.

So I did type casting for [AnyObject] I don't know it is correct or not but it still gives me same exception at runtime.

Edit:

What I did for Objective C code was:

- (void) removeMapOverlay {
    [self.mapView removeOverlays:[self.mapView overlays]];
    
    NSMutableArray *tempArray = [NSMutableArray arrayWithArray:[self.mapView annotations]];
    if ([tempArray containsObject:[MKUserLocation class]]) {
        [tempArray removeObject:[MKUserLocation class]];
    }
    
    NSArray *annotationArray = [NSArray arrayWithArray:tempArray];
    tempArray = nil;
    [self.mapView removeAnnotations:annotationArray];
}

I tried to create similar method in Swift. But it throws an exception like I explain above.

like image 460
Kampai Avatar asked Dec 03 '14 13:12

Kampai


4 Answers

I've just done this:

let overlays = mapView.overlays
mapView.removeOverlays(overlays)

and it seems to work. Why do you define your overlays as a static variable?

EDIT:

This is what we've done to be able to use the global contains function to search a swift array and check if the element we're looking for exists inside the array or not. In this case we were searching for CLLocationCoordinate2D inside an array.

public func == (lhs: CLLocationCoordinate2D, rhs: CLLocationCoordinate2D) -> Bool {
return lhs.longitude == rhs.longitude && lhs.latitude == rhs.latitude
}

public func == (lhs: MKMapPoint, rhs: MKMapPoint) -> Bool {
    return lhs.x == rhs.x && lhs.y == rhs.y
}

extension CLLocationCoordinate2D: Equatable{

}

//This is needed to be Equatable to use the global contains function
extension MKMapPoint: Equatable {

}
like image 164
C0D3 Avatar answered Nov 08 '22 00:11

C0D3


This worked for me:

self.mapView.overlays.forEach {
    if !($0 is MKUserLocation) {
        self.mapView.removeOverlay($0)
    }
}
like image 39
Mehmet Avatar answered Nov 08 '22 02:11

Mehmet


Import MapKit

private var appleMapView = MKMapView()

Here is the function to remove MKMapView Overlays + Annotations (Markers)

◙ func removeAppleMapOverlays(_ shouldRemoveMarkers : Bool? = false) {
    let overlays = self.appleMapView.overlays
    self.appleMapView.removeOverlays(overlays)
    if shouldRemoveMarkers == true {
        let annotations = self.appleMapView.annotations.filter {
            $0 !== self.appleMapView.userLocation
        }
        self.appleMapView.removeAnnotations(annotations)
    }
    
}

if you don't want to remove Markers, just call it as:

removeAppleMapOverlays()

and if you want to remove markers also, just call it as:

removeAppleMapOverlays(true)

like image 3
Haseeb Javed Avatar answered Nov 08 '22 00:11

Haseeb Javed


For google maps iOS mapping (Swift 3 and latest iOS SDK), Documentation, you will set the overlay as nil like so

overlay.map = nil
like image 1
Micah Montoya Avatar answered Nov 08 '22 00:11

Micah Montoya