【问题标题】:Draw more than one group of lines in MKMapView using MKPolyline使用 MKPolyline 在 MKMapView 中绘制多组线
【发布时间】:2013-01-29 05:42:43
【问题描述】:

我正在使用这段代码来绘制一组线:

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 with other group like first example , another array } 可能像这样:

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];
}

但我得到了这个例外:

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '***
 -[NSRegularExpression enumerateMatchesInString:options:range:usingBlock:]: nil argument'

【问题讨论】:

  • 您确定显示的代码发生了错误吗?您希望每个组成为单独的叠加层还是应该将它们连接成一条线?
  • 好吧@AnnaKarenina,每组线,没有连接,只有那组线。我的意思是,第一组可能是一个多边形,但下一组是正方形,但在 MKMapView 的另一个位置,所以我猜它是另一个叠加层,但是是的,我调试并来自该代码......但没有解释...... .

标签: ios objective-c mkmapview mapkit polyline


【解决方案1】:

有两个问题:您提供的代码中的错误和异常。

代码错误

在完成puntitos 结构的构建之前,您正在为所有sumaCount 点添加叠加层(例如,第一次调用addOverlay,您只添加了[a1 count] 点)。您还假设a1a2a3rutas 中的所有项目。如果您希望它们连接,您应该:

  • addOverlay 退出循环;和
  • 不要使用a1a2a3,而是通过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(只要打开它就可以了)。

【讨论】:

  • 谢谢你的回答,我现在明白我的bug了,而且我没有使用正则表达式,但我认为使用sumaCount的方式是错误的,明天我会选择答案,并且两个都不错,谢谢。
  • 嘿@Rob,它 100% 正常工作,没有崩溃 我非常感谢你的帮助,真的。但我认为我必须解决一个“新”问题,在此之前我需要在绘制新点之前删除“旧”覆盖。这就是我使用self.routeLine 的原因,但我会自己弄清楚,我现在想怎么做。谢谢。
  • @rokimoki 非常好。如果您对如何删除以前的叠加层感到困惑,请告诉我们。但您绝对不需要为此保留self.routeLine
  • 嗯,为什么我不能拥有 self.routeLine?好吧,实际上我做了一个 NSMutableArray,所有 routeLine 添加到了覆盖中,然后如果需要我删除所有覆盖并清理数组。
  • @rokimoki 如果你愿意,你可以。我只是建议您应用奥卡姆剃刀并询问您是否需要。如果您需要它,那么请务必这样做。但这只是一个让您的个人MKPolyline 数组和MKMapView 数组overlays 不同步的机会。 MKMapView 提供了各种维护overlays 的方法(例如addOverlayaddOverlaysremoveOverlayremoveAllOverlays)。您可以使用MKMapView 属性overlays 和所有相关方法完成您想要的吗?如果没有,则创建自己的数组并保持两者同步。
【解决方案2】:

如果该错误确实来自显示的代码,则您可能正在使用某个变量的已释放实例。

如果您要一次添加多个叠加层,您应该停止使用routeLinerouteLineView 属性,它们一次只能保存一个引用。

在本地声明并创建 MKPolylineMKPolylineView 变量。

第二部分代码的主要问题是它为rutas 中的每个array 调用addOverlay,但puntitos 数组和sumaCount 不断累积来自all的值em> array 对象(例如,覆盖 1 只是第一个 array,覆盖 2 是第一个和第二个 array 的组合,等等)。

如果您想为rutas 中的每个array 单独覆盖,则应为每个 array 声明和初始化puntitossumaCount(即放入第一个for循环)。下面是一个例子:

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 *arrayLine = [MKPolyline polylineWithCoordinates: puntitos count: sumaCount];
    [self.mapView addOverlay: arrayLine];
}


相反,如果您想要一个组合所有 array 对象的叠加层,请将 MKPolyline 创建和 addOverlay after 放在主 for 循环中(并保留 puntitossumaCount 主 for 循环之前)。例如:

int sumaCount = 0;
CLLocationCoordinate2D puntitos[sumaCount];
int c = 0;
for (NSArray *array in rutas)
{
    sumaCount = sumaCount + [array count];

    for (CLLocation *cada in array)
    {
        puntitos[c] = CLLocationCoordinate2DMake(cada.coordinate.latitude, cada.coordinate.longitude);
        c++;
    }
}

MKPolyline *rutasLine = [MKPolyline polylineWithCoordinates: puntitos count: sumaCount];
[self.mapView addOverlay: rutasLine];


viewForAnnotation 中,请记住将代码更改为使用routeLinerouteLineView(您应该去掉这些以避免混淆)。例如:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id < MKOverlay >)overlay
{
    MKPolylineView *pv = [[[MKPolylineView alloc] initWithPolyline:overlay] autorelease];
      //remove autorelease if not using ARC

    pv.strokeColor = [UIColor redColor];
    pv.lineWidth = 5;

    return pv;
}

【讨论】:

  • 非常感谢您的快速回答非常感谢您的帮助!
  • 您的代码帮助了我@AnnaKarenina,但我认为@Rob 对我的帮助更多,但- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id &lt; MKOverlay &gt;)overlay 的澄清帮助我让每组线条具有不同的颜色=) 谢谢!
猜你喜欢
  • 1970-01-01
  • 2014-02-25
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多