问题描述
我已经尝试了几乎所有方法来显示图像而不是 MKMapView 上的默认红色图钉.
I have tried just about everything to get an image displayed instead of the default red pin on my 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;
}
这对我没有任何帮助,从表面上看它只是一种方法.
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.
我在哪里称呼它?我该怎么称呼它?
谢谢!
推荐答案
我自己设法解决了这个问题,这是我分 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;
}
Step3:创建一个名为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];
我希望这会有所帮助!
这篇关于iOS 中 MKMapView 上的自定义引脚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!