上的MKMapView自销的iOS? [英] Custom pin on MKMapView in iOS?
问题描述
我已经尝试了刚才的一切得到显示的图像,而不是默认的红针在我的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;
}
第三步:创建一个名为说文解字注(或任何其他名称)班
的 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
第四步:添加注释,你有你自己的自定义图像
的 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];
我希望这有助于!
I hope this helps!
这篇关于上的MKMapView自销的iOS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!