1

私は Xcode を初めて使用し、基本的には、MKMapView各ピンに個別の色を追加したいと考えています。現在、ピンに注釈を付けています。

LocationAnnotation.h:

@interface LocationAnnotation : NSObject <MKAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

@end

LocationAnnotation.m:

#import "LocationAnnotation.h"

@implementation LocationAnnotation
@synthesize coordinate, title, subtitle;

@end

私の各座標は、MainMapViewController.h で次のようになります。

#define TheKingBill_LATITUDE 50.431379
#define TheKingBill_LONGITUDE -3.685495

 //Annotation

NSMutableArray * locations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
LocationAnnotation * myAnn;

//The King Bill Annotation
myAnn = [[LocationAnnotation alloc] init];
location.latitude = TheKingBill_LATITUDE;
location.longitude = TheKingBill_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = @"The King Bill";
myAnn.subtitle = @"Another Pub In Town";
[locations addObject:myAnn];

 //The Seven Stars Annotations
myAnn = [[LocationAnnotation alloc] init];
location.latitude = TheSevenStars_LATITUDE;
location.longitude = TheSevenStars_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = @"The Royal Seven Stars Hotel";
myAnn.subtitle = @"Hotel In Town";
[locations addObject:myAnn];

 [self.MainMapView addAnnotations:locations];

各ピンの色を変えるために何を追加する必要があるかについてのアイデアはありますか?

ありがとう!


さて、この LocationAnnotation ファイルにリンクする他のマップに問題があります。これにもカラーピンを追加したいです。新しいコードが以前の mapviewcontroller と同じであれば理想的です。これがコードです...

LocationAnnotation.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface LocationAnnotation : NSObject <MKAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property int idNumber;

- (id)initWithTitle:(NSString *)ttl andSubtitle:(NSString *)subttl andCoordinate:(CLLocationCoordinate2D)c2d andID:(int)idN;

@end

LocationAnnotation.m

    #import "LocationAnnotation.h"

    @implementation LocationAnnotation

    @synthesize coordinate, title, subtitle, idNumber;

    - (id)initWithTitle:(NSString *)ttl andSubtitle:(NSString *)subttl andCoordinate:(CLLocationCoordinate2D)c2d andID:(int)idN
    {
        self = [super init];

        if (self)
        {
            self.title = ttl;
            self.coordinate = c2d;
            self.subtitle = subttl;
            self.idNumber = idN;
        }

        return self;
    }

    @end

MapViewController.h

    #import <UIKit/UIKit.h>
    #import <MapKit/MapKit.h>

    @interface MapViewController : UIViewController

    @property (weak, nonatomic) IBOutlet MKMapView *ClientMapView;

    @end

MapViewController.m

#import "MapViewController.h"
#import "LocationAnnotation.h"

@interface MapViewController ()

@end


//The Dartmouth Inn Coordinates
#define DARTMOUTH_INN_LATITUDE 50.430036;
#define DARTMOUTH_INN_LONGITUDE -3.683873;

//Span
#define THE_SPAN 0.004f;



@implementation MapViewController
@synthesize ClientMapView;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];


    //Create the region
    MKCoordinateRegion myRegion;

    //Centre
    CLLocationCoordinate2D centre;
    centre.latitude = DARTMOUTH_INN_LATITUDE;
    centre.longitude = DARTMOUTH_INN_LONGITUDE;

    //Span
    MKCoordinateSpan span;
    span.latitudeDelta = THE_SPAN;
    span.longitudeDelta = THE_SPAN;

    myRegion.center = centre;
    myRegion.span = span;

    //Set The Map View
    [ClientMapView setRegion:myRegion animated:YES];


    //Annotation

    //1. Create A Coordinate for use with annotation

    CLLocationCoordinate2D dartLocation;
    dartLocation.latitude = DARTMOUTH_INN_LATITUDE;
    dartLocation.longitude = DARTMOUTH_INN_LONGITUDE;


    LocationAnnotation * myAnnotation = [LocationAnnotation alloc];
    myAnnotation.coordinate = dartLocation;
    myAnnotation.title = @"The Dartmouth Inn";

    [self.ClientMapView addAnnotation:myAnnotation];




}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
4

1 に答える 1

1

わかりました、変更を加えたコードです:

LocationAnnotation.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface LocationAnnotation : NSObject <MKAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property int idNumber;

- (id)initWithTitle:(NSString *)ttl andSubtitle:(NSString *)subttl andCoordinate:(CLLocationCoordinate2D)c2d andID:(int)idN;

@end

LocationAnnotation.m

#import "LocationAnnotation.h"

@implementation LocationAnnotation

@synthesize coordinate, title, subtitle, idNumber;

- (id)initWithTitle:(NSString *)ttl andSubtitle:(NSString *)subttl andCoordinate:(CLLocationCoordinate2D)c2d andID:(int)idN
{
    self = [super init];

    if (self)
    {
        self.title = ttl;
        self.coordinate = c2d;
        self.subtitle = subttl;
        self.idNumber = idN;
    }

    return self;
}

@end

MainMapViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MainMapViewController : UIViewController <MKMapViewDelegate>

@property (weak, nonatomic) IBOutlet MKMapView *mainMapView;

@end

MainMapViewController.m

#import "MainMapViewController.h"
#import "LocationAnnotation.h"

@interface MainMapViewController ()

@end

//Totnes Main Centre Coordinates
#define Totnes_LATITUDE 50.433741
#define Totnes_LONGITUDE -3.685797

//The Dartmouth Inn Coordinates
#define DARTMOUTH_INN_LATITUDE 50.430036;
#define DARTMOUTH_INN_LONGITUDE -3.683873;

//Pub Offers Co-Ordinates

#define TheKingBill_LATITUDE 50.431379
#define TheKingBill_LONGITUDE -3.685495

#define TheSevenStars_LATITUDE 50.431045
#define TheSevenStars_LONGITUDE -3.682945

#define TheLordNelson_LATITUDE 50.430931
#define TheLordNelson_LONGITUDE -3.683644

//Span
#define THE_SPAN 0.01f;


@implementation MainMapViewController

@synthesize mainMapView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Set Delegate
    mainMapView.delegate = self;

    //Create the region
    MKCoordinateRegion myRegion;

    //Centre
    CLLocationCoordinate2D centre;
    centre.latitude = Totnes_LATITUDE;
    centre.longitude = Totnes_LONGITUDE;

    //Span
    MKCoordinateSpan span;
    span.latitudeDelta = THE_SPAN;
    span.longitudeDelta = THE_SPAN;

    myRegion.center = centre;
    myRegion.span = span;


    //Set The Map View
    [mainMapView setRegion:myRegion animated:YES];


    //Annotation

    NSMutableArray * locations = [[NSMutableArray alloc] init];
    LocationAnnotation * myAnn;

    //The King Bill Annotation
    myAnn = [[LocationAnnotation alloc] initWithTitle:@"The King Bill"
                                          andSubtitle:@"Another Pub In Town"
                                        andCoordinate:CLLocationCoordinate2DMake(TheKingBill_LATITUDE, TheKingBill_LONGITUDE)
                                                andID:1];
    [locations addObject:myAnn];

    //The Seven Stars Annotations
    myAnn = [[LocationAnnotation alloc] initWithTitle:@"The Royal Seven Stars Hotel"
                                          andSubtitle:@"Hotel In Town"
                                        andCoordinate:CLLocationCoordinate2DMake(TheSevenStars_LATITUDE, TheSevenStars_LONGITUDE)
                                                andID:2];
    [locations addObject:myAnn];

    //The Lord Nelson Annotations
    myAnn = [[LocationAnnotation alloc] initWithTitle:@"The Lord Nelson"
                                          andSubtitle:@"Great Pub In Centre of Town"
                                        andCoordinate:CLLocationCoordinate2DMake(TheLordNelson_LATITUDE, TheLordNelson_LONGITUDE)
                                                andID:3];
    [locations addObject:myAnn];

    [self.mainMapView addAnnotations:locations];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - MKMapViewDelegate

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation {
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];

    int annId = ((LocationAnnotation *)annotation).idNumber;
    annView.pinColor = (annId == 1) ? MKPinAnnotationColorPurple
                                    : (annId == 2) ? MKPinAnnotationColorGreen
                                                   : MKPinAnnotationColorRed;
    annView.canShowCallout = YES;
    return annView;
}
于 2013-04-08T09:57:07.877 に答える