remove annotations from mapview except for the users current location?

Discussion in 'iOS Programming' started by MACloop, Dec 17, 2009.

  1. MACloop macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #1
    How is it possible to remove all annotation on a mapview except for the current userLocation?
    I tried this:

    Code:
    	
    NSMutableArray *arr = [NSMutableArray alloc] initWithArray[startMap annotations]];
    
      for (MKAnnotation *a in [mapView annotations]) {
        if(a != mapView.userLocation) {
          [arr addObject:a];
        }
      }
    [mapView removeAnnotations:arr];
    

    But it does not work :-( Is there a way to detect/compare the current user location?
     
  2. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
  3. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    have you tried:
    Code:
    if(a.location != mapView.userLocation)
    
    also, depending on how many annotations you have or if that fits well with your code, rather than cycling thru all your annotations just to find the ones that aren't the user location, you could simply remove them all first and then add the user location afterwards.
     
  4. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #4
    That will not work I think. MKAnnotation has no property named location and mapView.userLocation is a MKUserLocation which inherites from MKAnnotation or am I wrong???? I just want to compare the annotation taken from the mapView with the users current location and delete all other annotations.
     
  5. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #5
    in my annotation object subclass i can get the annotations latitude and logitude thru CLLocationCoordinate2D.

    Code:
    - (CLLocationCoordinate2D)coordinate
    	{
    	CLLocationCoordinate2D theCoordinate = {self.latitude, self.longitude};
    	return theCoordinate;
    	}
    
    so you could do something like

    Code:
    if (a.coordinate != userLocation)
    
    sorry, i'm mostly thinking out loud here, and it's been a while since i used MKMapKit.
     
  6. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #6
    No problem! I am glad for any advice and will look at your example and how that could fit into my solution.
    Thanks!
     
  7. MACloop thread starter macrumors 6502

    Joined:
    May 18, 2009
    Location:
    Germany
    #7
    I have tried that to first remove all and then add the userLocation again. That did not work... :-(
     

Share This Page