Iphone 转换为NSArray-iOS sdk
有没有一个地方可以让我读到这篇文章,或者给我一个更好的例子,如何将这段代码更改为Iphone 转换为NSArray-iOS sdk,iphone,ios,uitableview,mkmapview,Iphone,Ios,Uitableview,Mkmapview,有没有一个地方可以让我读到这篇文章,或者给我一个更好的例子,如何将这段代码更改为NSArray -(void)loadOurAnnotations { CLLocationCoordinate2D workingCoordinate; workingCoordinate.latitude = -37.711455; //This has to be an integer workingCoordinate.longitude = 176.285013; //This has to
NSArray
-(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
上表示为注释
感谢您的帮助:-)
-杰夫如果我理解正确 首先:通过一些模态类将数据从plist文件读入NSArray。(下面的“位置”示例只是一个模态类,我通过核心数据填写) 第二:在“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];
}
我想这里有些混乱。纬度和经度不应该是整数,它们应该是双精度(CLLocationDegrees)。如果你把它转换成一个整数,你的读数会失去很多准确性。您特别希望在数组中设置什么?您展示的方法将不断生成相同的位置。你是想让这个方法每次调用时都返回一个
MyAnnotation
,然后将该MyAnnotation
添加到一个数组中吗?哦,我明白了-我想要实现的是添加更多的记录,比如上面的记录,然后在地图上显示为注释。然后我需要在tableview中显示标题,以便用户可以选择它们。我有一个记录列表,但它们都会被放在那个空白处——这样可以创建一个庞大的实现文件,因此希望将它们设置在一个合适的数组和plist中。希望这能澄清它??干杯-杰菲认为ubaltaci的回答很好地解释了这一点。如果您不想像在回答中所做的那样立即遍历它们,您可以更改LoadOutAnnotations
方法的签名以允许使用参数。这将允许您执行类似于loadOurAnnotations(lat、long、title、subtitle、annotationtype)的操作代码>,将参数替换为相关信息。在该方法中,它将是workingCoordinate.lation=lat;工作坐标。经度=长;等等。
这意味着您不再需要继续复制/粘贴代码,每次只需调用loadOurAnnotations(…)
。
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];
}