0

我正在尝试从数组中在我的 mapView 上显示多个注释。注释坐标从 XML 文件中解析出来,并以 [currentCall longitude] 和 [currentCall latitude] 的形式存储在数组中。我的问题是,“调用”数组的语法是什么?(我不知道你是不是这么说的)。在我的应用程序的另一部分,我在表格中显示解析的 XML 结果并使用“JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];” “调用”数组。如何显示我的注释?除了那一小部分,其他一切都很好。

这是实现文件:

@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
4

1 回答 1

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];
}
于 2012-08-28T03:00:38.133 回答