Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mkmapview MKUserLocation AnnotationView

I'm trying to create custom annotationviews for the annotations on my map. I'm doing that by adapting the protocol MKMapViewDelegate and overwriting the function mapView:viewForAnnotation:. It all works, the only problem is that I also have showsUserLocation set to TRUE, which means that one "Annotation" I get in my mapView:viewForAnnotation: method is of the class MKUserLocation.

I don't want the userlocation annotation to have my custom annotationview, I want that one to show the default userlocation annotationview! How do I return the default userlocation annotationview for the userlocation or exclude it from the annotations (that come in mapView:viewForAnnotation:)?

I have tried to catch the UserLocation in the mapView:viewForAnnotation: method, but I don't know what to return! (In this example I'm returning a standard MKAnnotationView, but that doesn't look like the default UserLocation Annotation (obviously).)

    if (![[annotation class] isEqual:[MKUserLocation class]]) {

        MKAnnotationView *view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"customAnnotation"];
        // edit the custom view
        return view;
    }

    MKAnnotationView *view = [[MKAnnotationView alloc] init];
    return view;
like image 959
Terrabythia Avatar asked Mar 17 '26 17:03

Terrabythia


2 Answers

to show the default annotation for user location just return nil for that case, I did it this way:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    // use your custom annotation
    if ([annotation isKindOfClass:[MyAnnotationClass class]]) {
        ...

        return annotationView;
    }

    // use default annotation
    return nil;
}
like image 111
tkanzakic Avatar answered Mar 19 '26 07:03

tkanzakic


Inside your viewForAnnotation method write this piece of code. Here the var 'map' is the outlet for your MKMapview;

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



        //Annoation View for current Location

        if(map.userLocation != annotation)

        {
            UIImage *image = [UIImage imageNamed:@"image.png"];
            annotation.image = image;

            return annotation; 


        }

        //Annotation View for current location

         return nil;

    }
like image 43
Xcoder Avatar answered Mar 19 '26 06:03

Xcoder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!