iOS中MKMapView如何设置自定义标记?

4
我尝试了几乎所有方法来在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
上面的方法是MapView的委托方法。你一直得到那个答案的原因是因为这是如何设置自定义图像的标记。只有在添加了地图视图并设置了委托后,它才会被调用一次。我建议你像这样跟随教程:http://www.techotopia.com/index.php/Working_with_Maps_on_the_iPhone_with_MapKit_and_the_MKMapView_Class。这将帮助你理解MapView和委托方法的工作原理。 - AdamM
稍微改述一下Adam的回答,你的MapView的代理需要实现那个方法。通常情况下,视图控制器被用作MapView的代理。 - Brendon
我仍在苦苦挣扎,我对iOS还很陌生,但我对实现方法需要做什么有相当好的理解,但是实现一个充当代理的方法似乎并不像我想象的那么简单。你知道有什么逐步教程,或者你能给我一个例子,告诉我如何做到这一点吗? - JakesRassie
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];
}

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

第三步:创建一个名为“Annotation”(或其他名称)的类

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

希望这可以帮到你!


1
非常感谢你的回答。我经历了同样的痛苦,因为我找不到一个完美的例子。你的代码帮了我很多忙。 - GenieWanted
2
这段代码是"可行的",但并不完美。首先,您应该使用可重用标识符来出列批注视图,这样该批注(类型)的视图就不会一直为所有引脚分配内存。如果您有许多具有复杂视图的批注,则性能可能会受到影响。祝贺! - cybercow

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接