iOS中MKMapView上的自定义引脚?

时间:2013-04-11 13:56:50

标签: objective-c annotations

我已尝试过几乎所有内容都可以显示图像而不是MKMapView上的默认红色图钉。

互联网上有很多关于此问题的答案,但他们都继续给我这个:


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

    NSString *annotationIdentifier = @"CustomViewAnnotation";
    MKAnnotationView* annotationView = [mapview dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
    if(!annotationView)
    {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                      reuseIdentifier:annotationIdentifier]];
    }
    annotationView.image = [UIImage imageNamed:@"map_location_pin.png"];
    annotationView.canShowCallout= YES;

    return annotationView;
}

这根本不能帮助我,它只是一种方法。

请告诉我如何使用它来使方法返回带有自定义图像的注释。 几乎所有其他答案都告诉我实施该方法而没有进一步的解释。

我在哪里打电话?我怎么称呼它?

谢谢!

1 个答案:

答案 0 :(得分:17)

管理我自己解决这个问题,这是我在4个步骤中所做的:

步骤1:您必须将mapview的委托设置为ViewController:

MapViewController.h

 @interface MapViewController : UIViewController <MKMapViewDelegate> {
        IBOutlet MKMapView *mapView;
    }
@property (nonatomic, nonatomic) IBOutlet MKMapView *mapView;

MapViewController.m

- (void)viewDidLoad
{    
    [super viewDidLoad];
    [mapView setDelegate:self];
}

第2步:实施方法:

MapViewController.m

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"];
    //MyPin.pinColor = MKPinAnnotationColorPurple;

UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[advertButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];

/*MyPin.rightCalloutAccessoryView = advertButton;
 MyPin.draggable = YES;

 MyPin.animatesDrop=TRUE;
 MyPin.canShowCallout = YES;*/
MyPin.highlighted = NO;
MyPin.image = [UIImage imageNamed:@"myCustomPinImage"];

return MyPin;
}

步骤3:创建一个名为“注释”(或任何其他名称)的类

Annotation.h

#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>

@interface Annotation : NSObject <MKAnnotation> {

    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;

}

@property(nonatomic, assign) CLLocationCoordinate2D coordinate;
@property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) NSString *subtitle;

@end

Annotation.m

#import "Annotation.h"

@implementation Annotation
@synthesize coordinate, title, subtitle;

@end

第4步:添加注释,您将获得自定义图像!

MapViewController.m

MKCoordinateRegion Bridge = { {0.0, 0.0} , {0.0, 0.0} };
        Bridge.center.latitude = [[[testArr objectAtIndex:updates] objectAtIndex:1] floatValue];
        Bridge.center.longitude = [[[testArr objectAtIndex:updates] objectAtIndex:0] floatValue];
        Bridge.span.longitudeDelta = 0.01f;
        Bridge.span.latitudeDelta = 0.01f;

        Annotation *ann = [[Annotation alloc] init];
        ann.title = @"I'm a pin";
        ann.subtitle = @"Your subtitle";
        ann.coordinate = Bridge.center;
        [mapView addAnnotation:ann];

我希望这有帮助!