mkmapview MKUserLocation AnnotationView

不羁岁月 提交于 2019-12-09 19:20:54

问题


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;

回答1:


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;
}



回答2:


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;

    }



回答3:


Create custom AnnotationView:

#import <MapKit/MapKit.h>

@interface AnnotationView : MKPlacemark

@property (nonatomic, readwrite, assign) CLLocationCoordinate2D coordinate;

@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *subtitle;

// you can put here any controllers that you want. (such like UIImage, UIView,...etc)

@end

And in .m file

#import "AnnotationView.h"

@implementation AnnotationView

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate addressDictionary:(NSDictionary *)addressDictionary
{
    if ((self = [super initWithCoordinate:coordinate addressDictionary:addressDictionary]))
    {
        self.coordinate = coordinate;
    }
    return self;
}

@end

// Use Annotation Add #import "AnnotationView.h" in your relevant .m file:

CLLocationCoordinate2D pCoordinate ;
pCoordinate.latitude = LatValue;
pCoordinate.longitude = LanValue;

// Create Obj Of  AnnotationView class  

AnnotationView *annotation = [[AnnotationView alloc] initWithCoordinate:pCoordinate addressDictionary:nil] ;

    annotation.title = @"I m Here";
    annotation.subtitle = @"This is Sub Tiitle";

[self.mapView addAnnotation:annotation];

Above is simple Example of how to create AnnotationView.




回答4:


If the object in the annotation parameter is an instance of the MKUserLocation class, you can provide a custom view to denote the user’s location. To display the user’s location using the default system view, return nil. If you do not implement this method, or if you return nil from your implementation for annotations other than the user location annotation, the map view uses a standard pin annotation view.

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

       // if it's the user location, just return nil or custom annotation view.
       if ([annotation isKindOfClass:[MKUserLocation class]]){
          return nil;
       } else {
          //return other annotations
       }

    }


来源:https://stackoverflow.com/questions/15494887/mkmapview-mkuserlocation-annotationview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!