2012-08-28 71 views
0

我想從我的mapView數組中顯示多個註解。註解座標從XML文件中解析並作爲[currentCall經度]和[currentCall緯度]存儲在數組中。我的問題是,「調用」數組的語法是什麼? (我不知道你是怎麼說的)。在我的應用程序的另一部分中,我將解析的XML結果顯示在一個表中,並使用「JointCAD * currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];」來「調用」數組。我如何做它顯示我的註釋?除了那一小部分,其他的一切都很好。iOS顯示數組中的註釋

下面是實現文件:

@implementation SecondViewController 
@synthesize mapView; 

XMLParser *xmlParser; 

-(IBAction)getlocation { 

mapView.showsUserLocation = YES; 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:1]; 
[UIView commitAnimations]; 

} 

-(IBAction)changeSeg:(id)sender { 

if (segment.selectedSegmentIndex == 0) { 
    mapView.mapType = MKMapTypeStandard; 
} 
if (segment.selectedSegmentIndex == 1) { 
    mapView.mapType = MKMapTypeSatellite; 
} 
if (segment.selectedSegmentIndex == 2) { 
    mapView.mapType = MKMapTypeHybrid; 
} 
} 

-(void)viewDidLoad { 

JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row]; 

mapView = [[MKMapView alloc] initWithFrame:self.view.bounds]; 
[self.view insertSubview:mapView atIndex:0]; 

[super viewDidLoad]; 
[mapView setMapType:MKMapTypeStandard]; 
[mapView setZoomEnabled:YES]; 
[mapView setScrollEnabled:YES]; 
[mapView setDelegate:self]; 

MKCoordinateRegion WCCCA = { {0.0, 0.0} , {0.0, 0.0} }; 
WCCCA.center.latitude = 45.53540820864449; 
WCCCA.center.longitude = -122.86178648471832; 
WCCCA.span.longitudeDelta = 0.02f; 
WCCCA.span.latitudeDelta = 0.02f; 
[mapView setRegion:WCCCA animated:YES]; 

Annotation *ann1 = [[Annotation alloc] init]; 
ann1.title = @"WCCCA"; 
ann1.subtitle = @"Washington County Consolidated Communications Agency"; 
ann1.coordinate = WCCCA.center; 
[mapView addAnnotation: ann1]; 

MKCoordinateRegion CALL = { {0.0, 0.0} , {0.0, 0.0} }; 
CALL.center.latitude = [currentCall.latitude doubleValue]; 
CALL.center.longitude = [currentCall.longitude doubleValue]; 
CALL.span.longitudeDelta = 0.02f; 
CALL.span.latitudeDelta = 0.02f; 
[mapView setRegion:WCCCA animated:YES]; 

Annotation *ann2 = [[Annotation alloc] init]; 
ann2.title = [currentCall currentCallType]; 
ann2.subtitle = [currentCall location]; 
ann2.coordinate = CALL.center; 
[mapView addAnnotation: ann1]; 

} 

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { 
MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"]; 
MyPin.pinColor = MKPinAnnotationColorRed; 

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

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

return MyPin; 
} 

@end 

回答

0

如果我正確理解你的代碼,你應該能夠通過你們解析XML迭代(看來該輸出從XMLPARSER對象是一個NSArray )並將註釋添加到循環內的mapView中。

您可以使用C函數CLLocationCoordinate2DMake()爲您的註釋創建座標數據結構。

NSArray *callsArray = [xmlParser calls]; 

for (JointCAD *call in callsArray) { 
    Annotation *ann = [[Annotation alloc] init]; 
    ann.title = [call currentCallType]; 
    ann.subtitle = [call location]; 
    ann.coordinate = CLLocationCoordinate2DMake([call latitude], [call longitude]); 
    [mapView addAnnotation:ann]; 
} 
+0

當我嘗試,我得到i386硬件架構下一個未定義的符號: 「_CLLocationCoordinate2DMake」,從引用: - [SecondViewController的viewDidLoad]在SecondViewController.o –

+0

沒關係,林遲鈍。我忘了將框架添加到項目中。 –

+0

工程很棒。非常感謝! –