4

I have tried just about everything to get an image displayed instead of the default red pin on my MKMapView.

There are a lot of answers about this on the internet but all of them keep giving me this:


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

This doesn't help me at all, its just a method by the looks of it.

Please tell me how to use this to make the method return a annotation with a custom image. Just about all the other answers tell me to implement that method with no further explaination.

Where do I call it? How do I call it?

Thanks!

4

1 回答 1

18

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

第 1 步:您必须将地图视图的委托设置为 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];
}

第二步:实现方法:

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

Step3:创建一个名为“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

注释.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];

我希望这有帮助!

于 2013-04-12T12:22:59.050 回答