MapKit注释的自定义图像

时间:2014-04-16 17:49:44

标签: ios objective-c mapkit

我想使用我的Pin(非默认值)。

我尝试了上面的代码。首先,我可以看到默认引脚。当我触摸showMyLocation时,触摸了sightLocation后,我的自定义引脚会更改默认引脚。我想在第一个视图中显示我的自定义引脚。

TOCGSightAnnotation.h

@interface TOCGSightAnnotation : NSObject <MKAnnotation>
@property (strong, nonatomic) NSString *title;
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title;
@end

TOCGSightAnnotation.m

@implementation TOCGSightAnnotation

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

@end

TOCGSightseeingMapKitViewController.h

@interface TOCGSightseeingMapKitViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>

@property (strong) NSNumber *latitude;
@property (strong) NSNumber *longitude;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (strong, nonatomic) CLLocationManager * locationManager;

@end

TOCGSightseeingMapKitViewController.m

#import "TOCGSightseeingMapKitViewController.h"
#import "TOCGSightAnnotation.h"

CLLocationCoordinate2D sightCoordinate;
MKCoordinateRegion region;

@interface TOCGSightseeingMapKitViewController ()

@end

@implementation TOCGSightseeingMapKitViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    [self.locationManager startUpdatingLocation];

    sightCoordinate = CLLocationCoordinate2DMake([self.latitude doubleValue], [self.longitude doubleValue]);

    TOCGSightAnnotation *annotation = [[TOCGSightAnnotation alloc] initWithCoordinate:sightCoordinate title:@"Sight Title"];
    [self.mapView addAnnotation:annotation];

    region =
    MKCoordinateRegionMakeWithDistance(sightCoordinate, 500, 500);

    [self.mapView setRegion:region animated:YES];
}

- (IBAction)myLocation:(id)sender
{
    self.mapView.delegate = self;
    self.mapView.showsUserLocation = YES;
    [self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
}

- (IBAction)sightLocation:(id)sender
{
    [self.mapView setRegion:region animated:YES];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView
            viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    if ([annotation isKindOfClass:[TOCGSightAnnotation class]])
    {
        MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView
                                                                 dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];

        if (!pinView)
        {
            pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                      reuseIdentifier:@"CustomPinAnnotationView"];
            pinView.image = [UIImage imageNamed:@"mapIcon.png"];
            pinView.pinColor = MKPinAnnotationColorRed;
            pinView.animatesDrop = YES;
            pinView.canShowCallout = YES;

        }
        else
            pinView.annotation = annotation;

        return pinView;
    }

    return nil;
}

2 个答案:

答案 0 :(得分:1)

原因很简单。方法

- (MKAnnotationView *)mapView:(MKMapView *)mapView
            viewForAnnotation:(id <MKAnnotation>)annotation
只有在设置MKMapViewDelegate时才会调用

。只要您没有触摸调用IBAction的按钮

- (IBAction)myLocation:(id)sender

未设置委托,因此不会调用委托方法。尝试移动线

self.mapView.delegate = self;
你的MKMapView分配后

答案 1 :(得分:0)

我试过这个并且有效。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    [self.locationManager startUpdatingLocation];
}

- (void)viewDidAppear:(BOOL)animated
{
    self.mapView.delegate = self;

    sightCoordinate = CLLocationCoordinate2DMake([self.latitude doubleValue], [self.longitude doubleValue]);

    TOCGSightAnnotation *annotation = [[TOCGSightAnnotation alloc] initWithCoordinate:sightCoordinate title:@"Sight Title"];
    [self.mapView addAnnotation:annotation];

    region =
    MKCoordinateRegionMakeWithDistance(sightCoordinate, 500, 500);

    [self.mapView setRegion:region animated:YES];
}