Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>routeLine</code> and <code>routeLineView</code> variables can only point to one overlay and overlay view at a time so you would theoretically need an array of such variables.</p> <p>However, based on the code shown, you don't need to keep references to these in the first place.</p> <p>I suggest removing the <code>routeLine</code> and <code>routeLineView</code> variables.</p> <p>Then you just create an overlay object locally and in <code>viewForOverlay</code> return a view for the requested <code>overlay</code> parameter.</p> <p>In the <code>loadRoute</code> method, you can loop through the <code>routesArray</code> and for each route, create a local overlay object, and call <code>addOverlay</code> on it.</p> <p>There's also an easier way to construct a map rect that bounds all the routes so you can set the map's region to show them all.</p> <p>Example:</p> <pre><code>-(void) loadRoute { _routeRect = MKMapRectNull; for (int raIndex = 0; raIndex &lt; routesArray.count; raIndex++) { //So we grab an array that holds all the locations of one route. NSArray *locations = [[NSArray alloc] initWithArray: [routesArray objectAtIndex:raIndex]]; //note we are getting object at raIndex (not 0) above //...no change to existing code here... //self.routeLine = [MKPolyline polylineWithPoints:pointArr count:[locations count]]; //replace above line with this... MKPolyline *pl = [MKPolyline polylineWithPoints:pointArr count:[locations count]]; [mapView addOverlay:pl]; //Zoom in to fit route on screen //_routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y); //replace above line with this to create a rect around ALL routes... if (MKMapRectIsNull(_routeRect)) _routeRect = pl.boundingMapRect; else _routeRect = MKMapRectUnion(_routeRect, pl.boundingMapRect); // clear the memory allocated earlier for the points free(pointArr); } } </code></pre> <p>In <code>viewDidLoad</code>, just call <code>loadRoute</code> and don't call <code>addOverlay</code>.</p> <p><br> The <code>viewForOverlay</code> method becomes much simpler:</p> <pre><code>- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id &lt;MKOverlay&gt;)overlay { MKPolylineView *pv = [[MKPolylineView alloc] initWithPolyline:overlay]; pv.fillColor = [UIColor redColor]; pv.strokeColor = [UIColor redColor]; pv.lineWidth = 3; return pv; } </code></pre> <p><br> By the way, in <code>loadRoute</code>, this line:</p> <pre><code>MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) *[locations count]); </code></pre> <p>should be:</p> <pre><code>MKMapPoint* pointArr = malloc(sizeof(MKMapPoint) *[locations count]); </code></pre> <p>It should use the size of <code>MKMapPoint</code> (not <code>CLLocationCoordinate2D</code>).<br> They are not the same thing (even though the structs happen to be the same byte size).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload