我有一个新的问题,我不知道为什么.我的另一个解决方案是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模拟器后,它就不工作了。
编辑:我得到这个错误:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'发布于 2013-03-20 01:43:18
我有几个想法:
mapView的IBOutlet是否已连接?如果self.mapView是nil,应用程序可能会崩溃。annotation.coordinate,以确保得到的结果是有效的?也许有一个MKUserLocation还没有有效的值,我知道我是那个给你们这个例程的人,但是我注意到我们没有检查没有位置的情况。你可能想要这样的东西:if ( annotation count > 0) {annotation region;if (annotation count > 1) { region = self regionForAnnotations:self.mapView.annotations;region = MKCoordinateRegionMake(region.center,MKCoordinateSpanMake(region.span.latitudeDelta * 1.05,region.center* 1.05));//将区域扩展5%Else{ id self.mapView.annotations= self.mapView.annotations;region = MKCoordinateRegionMakeWithDistance(annotation.coordinate,100.0,100.0);} self.mapView设置region:region动画:是;}
parser.attributeNames = @@"img src=";
这是在检索您的图像URL吗?我本以为这应该是:
parser.attributeNames =@@“源”;
也许您已经对解析器做了一些更改,但是didStartElement的attributeDict永远不会有一个以img src=为键的对象。如果XML标记是<img src="http://blah.blah.blah/0.jpg">,那么您要查找的属性名称就是src.
发布于 2013-03-20 07:02:27
你有没有检查过在这一点上你实际上有一个注解要使用?也许你的self.mapView.annotations[0]返回了nil。
https://stackoverflow.com/questions/15505573
复制相似问题