我正在尝试分配两个自定义注释,一个称为“ arsenal.png”,另一个称为“ chelsea.png”
使用Apple Mapkit框架,xcode 7。
需要代码帮助以实现自定义标记。
这是我的实现文件:
// TrucksViewController.m
#import "TrucksViewController.h"
#import "Annotation.h"
@interface TrucksViewController ()
@end
//Wimbledon Coordinates
#define WIMB_LATITUDE 51.434783;
#define WIMB_LONGITUDE -0.213428;
//Stadium Coordinates
#define ARSENAL_LATITUDE 51.556899;
#define ARSENAL_LONGITUDE -0.106483;
#define CHELSEA_LATITUDE 51.481314;
#define CHELSEA_LONGITUDE -0.190129;
//Span
#define THE_SPAN 0.20f;
@implementation TrucksViewController
@synthesize myMapView;
#pragma mark -
#pragma mark Initialisation
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
self.title = @"Trucks";
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}
return self;
}
#pragma mark -
#pragma mark UIViewController Delegates
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Update support iOS 7
if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
self.edgesForExtendedLayout = UIRectEdgeNone;
self.navigationController.navigationBar.translucent = NO;
}
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// Revert to default settings
if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
self.edgesForExtendedLayout = UIRectEdgeAll;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Create Region
MKCoordinateRegion myRegion;
//Center
CLLocationCoordinate2D center;
center.latitude = ARSENAL_LATITUDE;
center.longitude = ARSENAL_LONGITUDE;
//Span
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
//Region
myRegion.center = center;
myRegion.span = span;
//Set our mapview
[myMapView setRegion:myRegion animated:YES];
//Annotation
NSMutableArray * locations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
Annotation * myAnn;
//Arsenal annotation
myAnn = [[Annotation alloc] init];
location.latitude = ARSENAL_LATITUDE;
location.longitude = ARSENAL_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = @"Arsenal FC";
myAnn.subtitle = @"The Gunners";
[locations addObject:myAnn];
//Chelsea annotation
myAnn = [[Annotation alloc] init];
location.latitude = CHELSEA_LATITUDE;
location.longitude = CHELSEA_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = @"Chelsea FC";
myAnn.subtitle = @"The Blue Lions";
[locations addObject:myAnn];
[self.myMapView addAnnotations:locations];
}
@end
添加发布2013/12/11 18:31东部时间
更新了Annotations.h文件
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface Annotation : NSObject <MKAnnotation>
@property(nonatomic, assign) CLLocationCoordinate2D coordinate;
@property(nonatomic, copy) NSString * title;
@property(nonatomic, copy) NSString * subtitle;
@property(nonatomic, copy) NSString * imageName;
@end
更新了Annotations.m文件
#import "Annotation.h"
@implementation Annotation
@synthesize coordinate, title, subtitle, imageName;
@end
更新了TrucksViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface TrucksViewController : UIViewController {
}
@property (weak, nonatomic) IBOutlet MKMapView *myMapView;
@end
更新了TrucksViewController.m
(代码段)
//Arsenal annotation
myAnn = [[Annotation alloc] init];
location.latitude = ARSENAL_LATITUDE;
location.longitude = ARSENAL_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = @"Arsenal FC";
myAnn.subtitle = @"The Gunners";
myAnn.imageName = @"arsenal";
[locations addObject:myAnn];
//Chelsea annotation
myAnn = [[Annotation alloc] init];
location.latitude = CHELSEA_LATITUDE;
location.longitude = CHELSEA_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = @"Chelsea FC";
myAnn.subtitle = @"The Blue Lions";
myAnn.imageName = @"chelsea";
[locations addObject:myAnn];
[self.myMapView addAnnotations:locations];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
这是我更新的TrucksViewController.m文件:2013/12/11 20:07 Eastern
// TrucksViewController.m
#import "TrucksViewController.h"
#import "Annotation.h"
@interface TrucksViewController ()
@end
//Wimbledon Coordinates
#define WIMB_LATITUDE 51.434783;
#define WIMB_LONGITUDE -0.213428;
//Stadium Coordinates
#define ARSENAL_LATITUDE 51.556899;
#define ARSENAL_LONGITUDE -0.106483;
#define CHELSEA_LATITUDE 51.481314;
#define CHELSEA_LONGITUDE -0.190129;
//Span
#define THE_SPAN 0.20f;
@implementation TrucksViewController
@synthesize myMapView;
//insert code per Anna
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation: (id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[Annotation class]])
{
static NSString *reuseId = @"ann";
MKAnnotationView *av = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (av == nil)
{
av = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
}
else
{
av.annotation = annotation;
}
Annotation *ann = (Annotation *)annotation;
av.image = [UIImage imageNamed:ann.imageName];
return av;
}
//return nil (default view) if annotation is not our custom type
return nil;
}
//end new code per Anna
#pragma mark -
#pragma mark Initialisation
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
// Set the title for this view controller
// Note: In future we will copy over the title from any created UINavigationBar objects
self.title = @"Trucks";
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}
return self;
}
#pragma mark -
#pragma mark UIViewController Delegates
- (void)viewDidUnload {
[super viewDidUnload];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Update support iOS 7
if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
self.edgesForExtendedLayout = UIRectEdgeNone;
self.navigationController.navigationBar.translucent = NO;
}
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// Revert to default settings
if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
self.edgesForExtendedLayout = UIRectEdgeAll;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Create Region
MKCoordinateRegion myRegion;
//Center
CLLocationCoordinate2D center;
center.latitude = ARSENAL_LATITUDE;
center.longitude = ARSENAL_LONGITUDE;
//Span
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
//Region
myRegion.center = center;
myRegion.span = span;
//Set our mapview
[myMapView setRegion:myRegion animated:YES];
//Annotation
NSMutableArray * locations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
Annotation * myAnn;
//Arsenal annotation
myAnn = [[Annotation alloc] init];
location.latitude = ARSENAL_LATITUDE;
location.longitude = ARSENAL_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = @"Arsenal FC";
myAnn.subtitle = @"The Gunners";
myAnn.imageName = @"arsenal.png";
[locations addObject:myAnn];
//Chelsea annotation
myAnn = [[Annotation alloc] init];
location.latitude = CHELSEA_LATITUDE;
location.longitude = CHELSEA_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = @"Chelsea FC";
myAnn.subtitle = @"The Blue Lions";
myAnn.imageName = @"chelsea.png";
[locations addObject:myAnn];
[self.myMapView addAnnotations:locations];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
您确实需要实现viewForAnnotation
委托方法来显示注释的图像,而不是标准的红色图钉。
在该委托方法中,您需要创建一个MKAnnotationView
并将其image
属性设置为的图像返回给注解所需的图像。
您将获得对地图视图所要求的注释的引用,并使用注释的属性,相应地设置图像。
您可以基于注释的title
值,但是我建议采用以下方法:
将imageName
类型的属性添加NSString
到您的Annotation
类。将此属性设置为用于注释的图像名称,然后再调用addAnnotation
它。例如:
myAnn.title = @"Arsenal FC";
myAnn.subtitle = @"The Gunners";
myAnn.imageName = @"arsenal";
[locations addObject:myAnn];
在viewForAnnotation
委托方法中,检查注释是否为类型Annotation
,然后使用其imageName
属性设置注释视图的image
属性:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[Annotation class]])
{
static NSString *reuseId = @"ann";
MKAnnotationView *av = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (av == nil)
{
av = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
}
else
{
av.annotation = annotation;
}
Annotation *ann = (Annotation *)annotation;
av.image = [UIImage imageNamed:ann.imageName];
return av;
}
//return nil (default view) if annotation is not our custom type
return nil;
}
另外,不要忘记设置地图视图的delegate
属性,否则将不会调用委托方法。在Interface Builder中,将delegate
插座连接到视图控制器。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句