Draw more than one row group in MKMapView using MKPolyline

I use this code to draw one group of strings:

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

And it works for one row group that comes from NSArray *routes , but now I need more than one row group, for example NSMutableArray *routes = { NSArray with routes , NSArray with other group like first example , another array } , probably like this:

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

but I get this exception:

  *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSRegularExpression enumerateMatchesInString:options:range:usingBlock:]: nil argument' 
+4
source share
2 answers

There are two questions: Errors in your code and exception.

Code errors

You add an overlay for all sumaCount points until the puntitos structure is puntitos (for example, when addOverlay is called for the first time, you add only [a1 count] tags). You also think that a1 , a2 and a3 are all elements in rutas . If you want them to be connected, you must:

  • Extract addOverlay from the loop; and
  • Do not use a1 , a2 and a3 , but rather repeat through rutas to get sumaCount

Thus, if you really wanted to join the three groups of lines, you would:

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

If you are dealing with three separate row groups, you should:

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

Note that in this last example, I am not using your MKPolyline property, but the local var. If you need to save an array of these MKPolyline objects, just go ahead and do it, but for the purposes of the above code you don't need it. Honestly, in the first example, I probably lean towards you with the local variable MKPolyline . Why store it in a class property?!?

Exception

The above bug fixes are in your code, but your exception indicates another issue. This may be your code (since your first attempt to create MKPolyline uses sumaCount points, but only some of them). But the problem may be reset elsewhere, because the exception is not what you expect from code errors. Are you regexing elsewhere in your code? If you are sure that the problem is in the code itself, you can

 NSLog(@"rutas=%@", rutas); 

Personally, I would be surprised if the regex problem was caused by this, but the code in your original question could definitely cause some unexpected problems. I will fix the code and see if your exception is still happening. If so, add this rutas log statement, but better, find NSRegularExpression in your project and see where you can use it. You can also enable exception breakpoints (just enable it for all exceptions).

+2
source

If this error actually arises from the code shown, you can use a freed instance of some variable.

If you intend to add multiple overlays at the same time, you should stop using the routeLine and routeLineView , which can contain only a link to one.

Declare and create a local variable MKPolyline and MKPolylineView .

The main problem of the second section of code is that it calls addOverlay for each array in rutas , but the puntitos and sumaCount continue to accumulate with values ​​from all array objects (for example, overlay 1 is only the first array , overlay 2 is a combination of the first and second array etc.).

If you want a separate overlay for each array in rutas , puntitos and sumaCount must be declared and initialized for each array (i.e. placed inside the first for loop). The following is an example:

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


If instead you want to use a single overlay that combines all array objects, place the MKPolyline and addOverlay after the main for loop (and save the declaration and initialization of puntitos and sumaCount before the main for -loop). For instance:

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


In viewForAnnotation remember to change the code so that you don't use routeLine and routeLineView (you just have to get rid of them to avoid confusion). For instance:

 - (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; } 
+1
source

All Articles