Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li><p>As mentioned in the comments, your <code>locations</code> array is never allocated and initialized so it's <code>nil</code> and calls to it (like <code>addObject</code>) do nothing and so the polyline never gets any coordinates added to it (and so it doesn't show).</p> <p>In <code>viewDidLoad</code>, before starting the <code>CLLocationManager</code>, alloc and init the array:</p> <pre><code>self.locations = [NSMutableArray array]; </code></pre></li> <li><p>Another issue you will encounter is with this line in <code>didUpdateLocations</code> in the <code>for</code> loop:</p> <pre><code>CLLocation *currentLocation = [locations objectAtIndex:i]; </code></pre> <p>Here, <code>locations</code> (without the <code>self.</code>) refers to the delegate method's <strong><em>local parameter variable</em></strong> and not your <code>locations</code> <strong><em>class-instance-level property variable</em></strong>. The compiler must be warning you about this with a message like "Local declaration of 'locations' hides instance variable".</p> <p>In this case, the warning is critical. What you really mean to do here is reference the <code>locations</code> property variable where you are storing the user's complete trail of coordinates and not the local variable which only has the last x un-reported locations (usually only 1 object). </p> <p>So that line should be changed to:</p> <pre><code>CLLocation *currentLocation = [self.locations objectAtIndex:i]; </code></pre> <p>It would be better if you just used a different name than <code>locations</code> to avoid these problems.</p></li> <li><p>As also mentioned in the comments, since you are adding an overlay with the user's <em>complete</em> trail of motion every time the user moves, you need to remove the previous overlay first. Otherwise, you will needlessly be adding multiple overlays to the map since the last overlay covers the entire motion. The previous overlays are not obviously visible since they have the same coordinates, the same color, and the same line width. So before calling <code>addOverlay</code>, the simplest thing to do is call <code>removeOverlays</code> with map's current list of overlays:</p> <pre><code>//remove any previous overlays first... [self.mapView removeOverlays:mapView.overlays]; //now add overlay with the updated trail... [self.mapView addOverlay:pathPolyline]; </code></pre></li> <li><p>A minor point not affecting the display is that setting <code>fillColor</code> for a polyline has no effect. You only need to set <code>strokeColor</code> which the code is doing. You can remove the call to set <code>fillColor</code>.</p></li> <li><p>Finally, you may be interested in seeing <a href="https://developer.apple.com/library/ios/samplecode/Breadcrumb/Introduction/Intro.html" rel="nofollow">Apple's Breadcrumb sample app</a>. Their version uses a custom overlay which can be dynamically updated without having to remove and add overlays every time there's a change or addition.</p></li> </ol>
 

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