【问题标题】:Adding more annotations to UIMapView向 UIMapView 添加更多注释
【发布时间】:2012-10-18 13:53:48
【问题描述】:

我想为我的地图视图添加更多注释。我正在使用一个类Annotation,其中有一个方法addAnnotation。当我想添加一个新注释时,这个方法在我的UIViewController 中被调用。

但由于某种原因,只显示我最后添加的注释。

Annotation.h

#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>

@interface Annotation : NSObject {
CLLocationCoordinate2D cooridnate;
NSString *title;
NSString *subtitle;
}

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

@property (nonatomic, retain) NSMutableArray *locations;

-(void)addAnnotation: (NSString *)initTitle : (NSString *)initSubtitle : (CLLocationCoordinate2D) initCoordinate;

@end

Annotation.m

#import "Annotation.h"

@implementation Annotation

@synthesize coordinate, title, subtitle, locations;

-(void)addAnnotation: (NSString *)initTitle : (NSString *)initSubtitle : (CLLocationCoordinate2D) initCoordinate {

locations = [[NSMutableArray alloc] init];

self.coordinate = initCoordinate;
self.title = [NSString stringWithFormat:@"%@", initTitle];
self.subtitle = [NSString stringWithFormat:@"%@", initSubtitle];

[locations addObject:self]; // add all my anotations with location to an array

}

@end

我的 UIViewController 中的方法

#import "MapViewController.h"

@interface MapViewController ()

@end

@implementation MapViewController

@synthesize mapView;

#pragma mark - ViewController methods

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

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

[self initMapView];
ann = [[Annotation alloc] init]; // making place in memory
}

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

#pragma mark - UIMapController methods

- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView {
[self createAnnotations];
}

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

if ([annotation isKindOfClass:[MKUserLocation class]]) {
    //If annotation = user position ==> DON'T CHANGE
    return nil;
}

MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"];
MyPin.pinColor = MKPinAnnotationColorRed;

UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[advertButton addTarget:self action:@selector(showInfo:) forControlEvents:UIControlEventTouchUpInside];

MyPin.rightCalloutAccessoryView = advertButton;
MyPin.draggable = NO;
MyPin.highlighted = NO;
MyPin.animatesDrop= FALSE;
MyPin.canShowCallout = YES;

return MyPin;
}

#pragma mark - MY Methods

-(void) initMapView {
[mapView setMapType:MKMapTypeStandard]; // standaard maptype
[mapView setScrollEnabled:YES];
[mapView setZoomEnabled:YES];
[mapView setDelegate:self]; // gegevens van de map terugsturen naar deze viewController en de gebruiker
[mapView setShowsUserLocation:YES];
[self focusWorld];
}

-(void) createAnnotations {
[self initSomeExamples];
[self.mapView addAnnotations: ann.locations];

}

-(void) focusWorld {
MKCoordinateRegion worldRegion = MKCoordinateRegionForMapRect(MKMapRectWorld); // regio instellen op World
mapView.region = worldRegion; // regio doorgeven naar onze mapView
}

-(void) initSomeExamples {

// Washington
l1.latitude = 38.892102;
l1.longitude = -77.029953;

// Antwerp
l2.latitude = 51.219787;
l2.longitude = 4.411011;

// Egypt
l3.latitude = 29.993002;
l3.longitude = 31.157227;

// Brazil
l4.latitude = -22.900846;
l4.longitude = -43.212662;

[ann addAnnotation:@"America has a new President !" :@"Washington DC" :l1]; // call method to add an annotation with these parameters
[ann addAnnotation:@"Revolutionary app by Belgium student" :@"Antwerp" :l2];
[ann addAnnotation:@"The new Arabic revolution" :@"Egypt, Tahir square" :l3];
[ann addAnnotation:@"World Championchip football" :@"Rio de Janeiro" :l4];

}


#pragma mark - ACTIONS

-(void)showInfo:(id)sender {
NSLog(@"Show info");
}

@end

【问题讨论】:

    标签: objective-c ios xcode annotations mkannotation


    【解决方案1】:

    您不需要位置 @property 和 - addAnnotation: 方法。一个 MKAnnotation 对象代表一个位置。

    我的建议是相应地修改 Annotation 类,并符合并创建与您想要注释的位置数量一样多的对象。

    从 Annotation.h/m 中删除位置 @property 和 -addAnnotation: 方法

    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;
    
    -(id) initWithTitle: (NSString *)initTitle : (NSString *)initSubtitle : (CLLocationCoordinate2D) initCoordinate; 
    
    @end
    

    Annotation.m

    #import "Annotation.h"
    
    @implementation Annotation
    
    @synthesize coordinate, title, subtitle;
    
    -(id) initWithTitle: (NSString *)initTitle : (NSString *)initSubtitle : (CLLocationCoordinate2D) initCoordinate {
        self = [super init];
        if ( self ) {
            self.coordinate = initCoordinate;
            self.title = [NSString stringWithFormat:@"%@", initTitle];
            self.subtitle = [NSString stringWithFormat:@"%@", initSubtitle];
        }
        return self; // correction appreciated.
    }
    
    @end
    

    视图控制器中的一些方法发生了变化

    -(void) initSomeExamples {
    
    // Washington
    l1.latitude = 38.892102;
    l1.longitude = -77.029953;
    
    // Antwerp
    l2.latitude = 51.219787;
    l2.longitude = 4.411011;
    
    // Egypt
    l3.latitude = 29.993002;
    l3.longitude = 31.157227;
    
    // Brazil
    l4.latitude = -22.900846;
    l4.longitude = -43.212662;
    
    NSArray *annotations = @[
    [[Annotation alloc] initWithTitle:@"America has a new President !" :@"Washington DC" :l1],
    [[Annotation alloc] initWithTitle:@"Revolutionary app by Belgium student" :@"Antwerp" :l2],
    [[Annotation alloc] initWithTitle:@"The new Arabic revolution" :@"Egypt, Tahir square" :l3],
    [[Annotation alloc] initWithTitle:@"World Championchip football" :@"Rio de Janeiro" :l4] ];
    
    // Adding 4 annotation objects
    [self.mapView addAnnotations:annotations];
    
    }
    
    -(void) createAnnotations {
        [self initSomeExamples];
        // Annotations are added in -initSomeExamples method.
    }
    

    【讨论】:

    • +1 但 ivar cooridnate 拼写错误(OP 错误),应将 return self 放在 init 方法中,并且 NSArray *annotations = ... 行中的右括号可能太多。
    猜你喜欢
    • 2014-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多