LocationsViewController

LocationsViewController

我有这段代码在具有自定义图像的 map 上显示4个点。我已经开始了解到,也许我构造它的方式并不是最优的,但这正是我所拥有的。

请帮助我学习如何在有人点击时使用自定义poi将我创建的标题显示在 map 上。

我有两个使用视图的类。这些类是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

我想我需要一个函数来创建标题吗?我真的希望我不必创建更多的类来做到这一点。

最佳答案

您的主要问题是:

当有人点击时,如何使用自定义poi使我创建的标题显示在 map 上。

viewForAnnotation委托方法中,您需要将注释视图的canShowCallout属性设置为YES(默认为NO):

if (annotationView == nil)
{
    annotationView = [[MKAnnotationView alloc] initWithAnnotation...
    annotationView.canShowCallout = YES;  // <-- add this line
}

与该问题无关的一些其他评论:
  • 在MapAnnotation.h中,此类仅实现MKAnnotation,而不是MKMapViewDelegate,因此将其从协议列表中删除,以避免潜在的编译器警告,并使代码更清晰,更干净:
    @interface MapAnnotation : NSObject <MKAnnotation>
    
  • 在MapAnnotation.m中,您不需要#import "LocationsViewController.h"。去掉它。
  • 在LocationsViewController.h中,此类不会实现MKAnnotation,因此请将其从协议列表中删除,以避免潜在的编译器警告,并使代码更清晰,更干净:
    @interface LocationsViewController : UIViewController <MKMapViewDelegate>
    
  • 还是在LocationsViewController.h中,不必声明将要实现的委托方法。您可以删除viewForAnnotation声明。
  • 在LocationsViewController.m中,@synthesizecoordinatetitle不在这里。删除它们。
  • 关于ios - 在注释中添加标题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22336986/

    10-09 12:27