我有这段代码可以在带有自定义图像的 map 上显示 4 个点。我已经开始了解到,也许我构建它的方式不是最佳的,但它就是我所拥有的。
请帮助我学习如何让我创建的标题在有人点击时显示在 map 上并带有自定义 poi。
我有两个与 View 一起工作的类。这些类是 MapAnnotation 和 LocationsViewController。
这是每个的代码。
MapAnnotation.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapAnnotation : NSObject <MKAnnotation, MKMapViewDelegate>
{
CLLocationCoordinate2D _coordinate;
NSString * title;
}
@property (nonatomic, copy) NSString *title;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;
@end
MapAnnotation.m
#import "MapAnnotation.h"
#import "LocationsViewController.h"
@implementation MapAnnotation
@synthesize coordinate = _coordinate;
@synthesize title;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate
{
self = [super init];
if (self != nil)
{
_coordinate = coordinate;
}
return self;
}
@end
LocationsViewController.h
#import "TechBookAppDelegate.h"
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface LocationsViewController : UIViewController <MKMapViewDelegate, MKAnnotation>
{
IBOutlet MKMapView *worldView;
IBOutlet UIActivityIndicatorView *activityIndicator;
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation;
@end
LocationsViewController.m
#import "LocationsViewController.h"
#import "MapAnnotation.h"
@interface LocationsViewController ()
@end
@implementation LocationsViewController
@synthesize coordinate = _coordinate;
@synthesize title;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code
}
return self;
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationViewID = @"annotationViewID";
MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
}
annotationView.image = [UIImage imageNamed:@"app_icon_sm.png"];
annotationView.annotation = annotation;
return annotationView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
worldView.showsUserLocation = NO;
CLLocationCoordinate2D wisconsin = {.latitude = 45.620775, .longitude = -89.47844};
CLLocationCoordinate2D pennsyvainia = {.latitude = 39.912417, .longitude = -77.663268};
CLLocationCoordinate2D nevada = {.latitude = 36.054362, .longitude = -115.020470};
CLLocationCoordinate2D texas = {.latitude = 32.555038, .longitude = -94.295592};
MapAnnotation *pinWI = [[MapAnnotation alloc] initWithCoordinate: wisconsin] ;
MapAnnotation *pinPA = [[MapAnnotation alloc] initWithCoordinate: pennsyvainia];
MapAnnotation *pinNV = [[MapAnnotation alloc] initWithCoordinate: nevada];
MapAnnotation *pinTX = [[MapAnnotation alloc] initWithCoordinate: texas];
//UIImage *star = [UIImage imageNamed:@"locations_marker.png"];
//////////////////
pinWI.title = @"Rhinelander, WI";///////
pinPA.title = @"Chambersburg, PA";//////I dont understand why these do not output a
pinNV.title = @"Henderson, NV";/////////title if the title property is a part of the
pinTX.title = @"Marshall, TX";////////// MKAnnotation
//////////////////
[worldView addAnnotation:pinTX];
[worldView addAnnotation:pinNV];
[worldView addAnnotation:pinWI];
[worldView addAnnotation:pinPA];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我想我需要一个函数来创建标题?我真的希望我不必创建更多类来执行此操作。
最佳答案
您的主要问题是:
How to make the titles I created show up on the map with the custom poi when someone taps it.
在viewForAnnotation
委托(delegate)方法中,需要将注解 View 的canShowCallout
属性设置为YES
(默认为NO
):
if (annotationView == nil)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation...
annotationView.canShowCallout = YES; // <-- add this line
}
一些与问题无关的附加评论:
在 MapAnnotation.h 中,此类将仅实现
MKAnnotation
,而不是MKMapViewDelegate
,因此将其从协议(protocol)列表中删除以避免潜在的编译器警告并使代码更清晰:@interface MapAnnotation : NSObject <MKAnnotation>
在 MapAnnotation.m 中,您不需要
#import "LocationsViewController.h"
。将其删除。在 LocationsViewController.h 中,此类将不会实现
MKAnnotation
,因此将其从协议(protocol)列表中删除以避免潜在的编译器警告并使代码更清晰和清洁工:@interface LocationsViewController : UIViewController <MKMapViewDelegate>
此外,在 LocationsViewController.h 中,无需声明您将要实现的委托(delegate)方法。您可以删除
viewForAnnotation
声明。在 LocationsViewController.m 中,
coordinate
和title
的@synthesize
不属于此处。删除它们。
关于ios - 为注释添加标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22336986/