我遇到了一个新问题,我不知道为什么...我进一步的解决方案是 Rob 发布的 here。我喜欢他的作品,在更新到 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'
最佳答案
几个想法:
您检查过以确保您的
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
永远不会有一个由img src=
键控的对象.如果 XML 标记是<img src="http://blah.blah.blah/0.jpg">
,您要查找的属性名称就是src
.
关于ios - MKCoordinateRegionMakeWithDistance 抛出 "Thread 1: signal SIGABRT",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15505573/