我正在使用这段代码绘制一组线条:
CLLocationCoordinate2D points[[routes count]];
for(int i = 0; i < self.routes.count; i++)
{
CLLocation* location = [self.routes objectAtIndex:i];
points[i] = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate
.longitude);
}
self.routeLine = [MKPolyline polylineWithCoordinates: points count: [routes count]];
[self.mapView setVisibleMapRect: [self.routeLine boundingMapRect] animated: YES];
[self.mapView addOverlay:self.routeLine];
它适用于来自 NSArray *routes
的一组行,但现在我需要不止一组行,例如 NSMutableArray *routes = { NSArray with routes , NSArray 与其他组像第一个例子,另一个数组
可能是这样的:
int sumaCount = [a1 count] + [a2 count] + [a3 count];
CLLocationCoordinate2D puntitos[sumaCount];
int c = 0;
for (NSArray *array in rutas)
{
for (CLLocation *cada in array)
{
puntitos[c] = CLLocationCoordinate2DMake(cada.coordinate.latitude, cada.coordinate.longitude);
c++;
}
self.routeLine = [MKPolyline polylineWithCoordinates: puntitos count: sumaCount];
[self.mapView addOverlay: self.routeLine];
}
但我得到了这个异常(exception):
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '***
-[NSRegularExpression enumerateMatchesInString:options:range:usingBlock:]: nil argument'
最佳答案
有两个问题:您提供的代码中的错误和异常。
代码错误
在完成 puntitos
结构的构建之前,您正在为所有 sumaCount
点添加覆盖(例如,第一次调用 addOverlay
,您只添加了 [a1 count]
点)。您还假设 a1
、a2
和 a3
都是 rutas
中的项目。如果你想让它们连接起来,你应该:
- 将
addOverlay
退出循环;和 - 不要使用
a1
、a2
和a3
,而是通过rutas
进行迭代以获得sumaCount
因此,如果你真的想将这三组线连接在一起,你会:
int sumaCount = 0;
for (NSArray *array in rutas)
sumaCount += [array count];
CLLocationCoordinate2D puntitos[sumaCount];
int c = 0;
for (NSArray *array in rutas)
{
for (CLLocation *cada in array)
{
puntitos[c] = CLLocationCoordinate2DMake(cada.coordinate.latitude, cada.coordinate.longitude);
c++;
}
}
self.routeLine = [MKPolyline polylineWithCoordinates: puntitos count: sumaCount];
[self.mapView addOverlay: self.routeLine];
如果您要处理三组不同的线,您应该:
for (NSArray *array in rutas)
{
int sumaCount = [array count];
CLLocationCoordinate2D puntitos[sumaCount];
int c = 0;
for (CLLocation *cada in array)
{
puntitos[c] = CLLocationCoordinate2DMake(cada.coordinate.latitude, cada.coordinate.longitude);
c++;
}
MKPolyline routeLine = [MKPolyline polylineWithCoordinates: puntitos count: c];
[self.mapView addOverlay: routeLine];
}
请注意,在后一个示例中,我没有使用您的 MKPolyline
属性,而是使用本地变量。如果您需要保留这些 MKPolyline
对象的数组,请继续这样做,但出于上述代码的目的,您不需要它。坦率地说,在第一个示例中,我可能也倾向于使用本地 MKPolyline
变量。为什么要将它保留在类属性中?!?
异常
上面修复了您列出的代码中的错误,但您的异常指向另一个问题。这可能是您的代码(因为您第一次尝试创建 MKPolyline
是使用 sumaCount
点,但只设置了其中的几个点)。但是问题也可能在其他地方重新出现,因为异常不是您对代码错误的期望。您是否在代码的其他地方进行任何正则表达式匹配?如果您确信问题出在代码本身,您可以
NSLog(@"rutas=%@", rutas);
就个人而言,如果正则表达式问题是由此引起的,我会感到惊讶,但您原始问题中的代码肯定会导致一些意想不到的问题。我会修复代码,看看您的异常是否仍然发生。如果是,请添加此 rutas
日志语句,但更好的是,在您的项目中搜索 NSRegularExpression
并查看您可能在哪里使用它。您也可以turn on exception breakpoints (只需为所有异常(exception)打开它)。
关于ios - 使用 MKPolyline 在 MKMapView 中绘制多组线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14857599/