我有一个新问题,我不知道为什么......我的进一步解决方案是here由Rob发布。我喜欢他的工作,并且在iOS 6.1更新之前它运行良好。
- (void)loadKml:(NSURL *)url
{
// parse the kml
Parser *parser = [[Parser alloc] initWithContentsOfURL:url];
parser.rowElementName = @"Placemark";
parser.elementNames = @[@"name", @"Snippet", @"coordinates", @"description"];
parser.attributeNames = @[@"img src="];
[parser parse];
// add annotations for each of the entries
for (NSDictionary *locationDetails in parser.items)
{
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.title = locationDetails[@"name"];
annotation.subtitle = locationDetails[@"Snippet"];
NSArray *coordinates = [locationDetails[@"coordinates"] componentsSeparatedByString:@","];
annotation.coordinate = CLLocationCoordinate2DMake([coordinates[1] floatValue], [coordinates[0] floatValue]);
[self.mapView addAnnotation:annotation];
}
// update the map to focus on the region that encompasses all of your annotations
MKCoordinateRegion region;
if ([self.mapView.annotations count] > 1)
{
region = [self regionForAnnotations:self.mapView.annotations];
region = MKCoordinateRegionMake(region.center, MKCoordinateSpanMake(region.span.latitudeDelta * 1.05, region.span.longitudeDelta * 1.05)); // expand the region by 5%
}
else
{
id<MKAnnotation> annotation = self.mapView.annotations[0];
region = MKCoordinateRegionMakeWithDistance(annotation.coordinate, 100.0, 100.0); // >>>this line throws: "Thread 1: signal SIGABRT"<<<
}
[self.mapView setRegion:region animated:YES];
}
自iOS 6.1 Simulator更新以来,它无法正常工作。
编辑:我收到此错误:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
答案 0 :(得分:1)
有几点想法:
您是否检查过以确保IBOutlet
mapView
与您self.mapView
挂钩?如果nil
为annotation.coordinate
,则应用可能会崩溃。
您是否看过MKUserLocation
以确保您获得了有效的结果?也许有if ([self.mapView.annotations count] > 0)
{
MKCoordinateRegion region;
if ([self.mapView.annotations count] > 1)
{
region = [self regionForAnnotations:self.mapView.annotations];
region = MKCoordinateRegionMake(region.center, MKCoordinateSpanMake(region.span.latitudeDelta * 1.05, region.span.longitudeDelta * 1.05)); // expand the region by 5%
}
else
{
id<MKAnnotation> annotation = self.mapView.annotations[0];
region = MKCoordinateRegionMakeWithDistance(annotation.coordinate, 100.0, 100.0);
}
[self.mapView setRegion:region animated:YES];
}
还没有有效值;
我知道我是那个例行公事的人,但我注意到我们没有检查没有位置的情况。你可能想要这样的东西:
parser.attributeNames = @[@"img src="];
顺便说一句,我注意到你正在使用:
parser.attributeNames = @[@"src"];
是检索图片网址吗?我原以为这应该只是:
attributeDict
也许您已对解析器进行了一些更改,但didStartElement
的{{1}}永远不会有img src=
键入的对象。如果XML标记为<img src="http://blah.blah.blah/0.jpg">
,则您要查找的属性名称仅为src
。
答案 1 :(得分:0)
你检查过你实际上有一个注释要在那时使用吗?也许你的self.mapView.annotations[0]
返回了无。