我想使用我的密码(不是默认值)。

我尝试了上面的代码。首先,我可以看到默认的引脚。当我触摸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;
}

最佳答案

原因很简单。方法

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


仅当您设置MKMapViewDelegate时才会被调用。只要您不触摸调用IBAction的按钮

- (IBAction)myLocation:(id)sender


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

self.mapView.delegate = self;


在您的MKMapView分配之后。

关于ios - MapKit注释的自定义图钉图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23116598/

10-09 10:11