mkmapview MKUserLocation AnnotationView

我与影子孤独终老i 提交于 2019-12-04 14:58:51

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

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;

    }

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.

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
       }

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