2011-12-30 46 views
0

有没有一个地方,我可以阅读或者更好地给我一个例子,如何将这个代码改为NSArray转换为NSArray - iOS sdk

-(void)loadOurAnnotations 
{ 
CLLocationCoordinate2D workingCoordinate; 

workingCoordinate.latitude = -37.711455; //This has to be an integer 
workingCoordinate.longitude = 176.285013; //This has to be an integer 
MyAnnotation *myLocation1 = [[MyAnnotation alloc] initWithCoordinate:workingCoordinate]; //The pointer has to be set to an array 
[myLocation1 setTitle:@"The Palms Cafe"]; //The pointer and the setTitle here 
[myLocation1 setSubtitle:@"157 Domain Road - (07) 542 2430"]; //The pointer and the setSubTitle here 
[myLocation1 setAnnotationType:MyAnnotationTypeMine]; //again the pointer here 

[mapView addAnnotation:myLocation1]; //and the pointer here 

} 

所有明显来自阵列和整片的代码(大括号内)在同一个地方的指针是一个记录,所以如果我想添加其他地方,我需要复制所有这一切再次。

所以我想实现的是将其设置在Plist中,以便我可以在其中添加记录,但只有在代码中设置一次-(void)loadOurAnnotations并重复其自身。当然,如果我放弃-(void)loadOurAnnotations那么这不是问题,它只是我现在拥有的方式。

正如您可能能够通过我收集的信息分辨的那样,这些将在MKMapView上表示为注释。

任何帮助表示赞赏:-)

-Jeff

+3

我认为这里有一些困惑。纬度和经度不应该是整数,它们应该是双倍(CLLocationDegrees)。如果您要将其转换为整数,您在阅读中会失去很多准确性。你想在数组中设置什么?你显示的方法将不断产生相同的位置。你是否试图让这个方法每次调用时都返回一个'MyAnnotation',然后将'MyAnnotation'添加到数组中? – keyboardP 2011-12-30 03:28:10

+0

哦,我看到了 - 我想要实现的是添加更多像上面那样的记录,然后将其显示为地图上的注释。那么我需要在桌面视图中显示标题,以便用户可以选择它们。我有一个记录列表,但它们全都放在void中 - 这样可以创建一个巨大的实现文件,因此希望将它们设置在适当的数组和plist中。希望澄清它? CHeers -Jeff – 2011-12-30 07:31:11

+0

我认为'ubaltaci's'的答案可以很好的解释它。如果你不想一次循环它们,就像在答案中做的那样,你可以改变'loadOutAnntations'方法的签名来允许参数。这将允许你做'loadOurAnnotations(lat,long,title,subtitle,annotationtype)'',用相关信息替换参数。在该方法中,它将是'workingCoordinate.latitude = lat; workingCoordinate.longitude = long;这意味着你不再需要复制/粘贴代码,你可以每次调用'loadOurAnnotations(...)'。 – keyboardP 2011-12-30 17:17:18

回答

0

如果我理解正确,

第一:你是通过模态类从plist文件读取数据到NSArray的。 (下面的“位置”只是一个模式类的例子,我通过核心数据填写)

// Location.h 
@interface Location : NSManagedObject 

@property (nonatomic, strong) NSNumber * longitude; 
@property (nonatomic, strong) NSNumber * latitude; 
@property (nonatomic, strong) NSString * name; 

@end 

// Location.h 
@implementation Location 

@dynamic longitude; 
@dynamic latitude; 
@dynamic name; 

@end 

二:在你的“loadOurAnnotations”的方法,迭代中包含位置对象和每个“locationArray”的所有值位置对象,我创建PinLocation实例(MkAnnotation的子类),并将它们添加到mapView中。

for(Location *location in locationArray) { 
    NSString *name = [location name]; 
    CLLocationCoordinate2D coordinate; 
    coordinate.latitude = [[location latitude] doubleValue]; 
    coordinate.longitude = [[location longitude] doubleValue]; 

    PinLocation *pinLocation = [[PinLocation alloc] initWithName:name coordinate:coordinate]; 
    [self.mapView addAnnotation:pinLocation];  
}