ios:mapA的addAnnotations无法正常工作

时间:2013-04-20 06:07:50

标签: ios mkmapview

您好我有一个带注释对象的NSMutableArray,下面的代码不会添加注释:

在.h我有

     @property (retain, nonatomic) MarketsDataController *marketList;

在.m中我有(添加注释不起作用。地图上没有显示任何内容)

    _marketList = [_marketService.marketsDataController retain];
    NSLog(@"%@!!!", [_marketList objectInListAtIndex:0].title);
    [mapView addAnnotation:[_marketList objectInListAtIndex:0]];
    [mapView addAnnotation:[_marketList objectInListAtIndex:1]];
    [mapView addAnnotation:[_marketList objectInListAtIndex:2]];

MarketsListDataController看起来像这样:

    #import "MarketsDataController.h"

    //The following interface is used for private methods
    @interface MarketsDataController ()
    - (void)initializeMarketsList;
    @end

    @implementation MarketsDataController

    //Set initial values for instance variables
    - (id)init {
        NSLog(@"init MarketsDataController");
        if (self = [super init]) {
            [self initializeMarketsList];
            return self;
        }
        return nil;
    }

    - (void)initializeMarketsList{
        NSMutableArray *marketsList = [[NSMutableArray alloc] init]; //Initialize the product list
        _marketsList = marketsList;                      //Set the markets list to the markets list
    }

    //Return the number of products
    - (NSUInteger)countOfList {
        return [_marketsList count];
    }

    //Return a product within the list
    - (MapAnnotation *)objectInListAtIndex:(NSUInteger)theIndex {
        return [_marketsList objectAtIndex:theIndex];
    }

    - (void)addAnnotationToList:(MapAnnotation *)mapAnnotation{
        NSLog(@"Adding market");
        [_marketsList addObject:mapAnnotation];
    }

    - (void)dealloc {
        NSLog(@"DEALLOC MarketsDataController");
        [_marketsList release];
        [super dealloc];

    }

    @end

MapAnnotation .m看起来像:

#import "MapAnnotation.h"

@implementation MapAnnotation

@synthesize coordinate, title, subtitle;

- (id)init{
    CLLocationCoordinate2D location;
    location.latitude = 0;
    location.longitude = 0;
    return [self initWithCoordinate:coordinate title:nil subtitle:nil];
}

- (id)initWithCoordinate:(CLLocationCoordinate2D) c title:(NSString *)t subtitle:(NSString *)st{
    self = [super init];
    coordinate = c;
    title = [t retain];
    subtitle = [st retain];
    return self;
}

- (void) dealloc{
    [title release];
    [subtitle release];
    [super dealloc];
}

@end

我创建它们并将它们添加到另一个类中:

if (![latitude isEqual:[NSNull null]] && ![longitude isEqual:[NSNull null]]) {
                     NSLog(@"%d", i);
                    NSLog(@"%@", title);
                    CLLocationCoordinate2D coordinate;
                    coordinate.longitude = [latitude doubleValue];
                    coordinate.longitude = [longitude doubleValue];
                    [self buildMarketsList:coordinate title:title subtitle:@""]; //build the browse list product

                }

方法如下:

- (void)buildMarketsList:(CLLocationCoordinate2D)c title:(NSString *)t subtitle:(NSString *)st{
    MapAnnotation *mapAnnotation = [[MapAnnotation alloc]initWithCoordinate:c title:t subtitle:st];
    [_marketsDataController addAnnotationToList:mapAnnotation];
    [mapAnnotation release];
}   

如何添加一个实现<的注释对象数组? MKAnnotation> ?我没有错误,没有注释会显示。

2 个答案:

答案 0 :(得分:2)

发现问题:

改变:

                coordinate.longitude = [latitude doubleValue];
                coordinate.longitude = [longitude doubleValue];

要:

                coordinate.latitude = [latitude doubleValue];
                coordinate.longitude = [longitude doubleValue];

答案 1 :(得分:0)

_marketsList不是带数组的对象。它一个数组。

如果您有一个名为marketsList的属性,该属性由名为_marketsList

的变量支持
@property (nonatomic, retain) NSMutableArray * marketsList; //in your .h


@synthesize marketsList = _marketsList; //in your .m
那么你应该这样做:

- (void)initializeMarketsList{
    marketsList = [[NSMutableArray alloc] init]; //Initialize the product list

    [marketsList addObject:myAnnotation];//ad some annotations
}

然后将您的注释数组添加到地图视图

[mapView addAnnotations:marketsList];