title: Gradually decrease polylines if user is following it in iPhone
body: <p>I am creating a Navigation iPhone app that creates a <code>polyline</code> between the <code>user location</code> and the <code>destination</code> the user wants to go. </p>
  
  <p>I am able to create the <code>polyline</code>. But what i need to do is as the user proceeds on the polyline, the polyline need to gradually decrease showing the user is on track. And i also need to detect if the user has strayed from the path. </p>
  
  <p>For example, if the user has to go left but he doesn't, how will i detect that?</p>
  
  <p>Any help will be appreciated. Thanks.</p>
  
  <p>EDIT :</p>
  
  <p>I have used the following code to draw the polyline for </p>
  
  <pre><code>-(void)drawPolyline{
      //Draw polyline via the selected route
  
      NSMutableArray *polys = [NSMutableArray array];
      GMSMutablePath *path = [GMSMutablePath path];
  
      NSDictionary *route=[routes objectAtIndex:0];
  
      //self.strPolyPoints is a string
      self.strPolyPoints=[[route objectForKey:@"overview_polyline"] objectForKey:@"points"];
      NSArray *arrPoints=[self decodePolyLine];
      for(CLLocation *location in arrPoints){
          CLLocationCoordinate2D coordinate=location.coordinate;
          [path addCoordinate:coordinate];
      }
  
      _lengths = @[@([path lengthOfKind:kGMSLengthGeodesic] / 40)];
      GMSPolyline *polyline = [[GMSPolyline alloc] init];
      polyline.path = path;
      polyline.strokeColor = [UIColor blueColor];
      polyline.geodesic = NO;
      polyline.strokeWidth = 5;
      polyline.map = mapView_;
      [polys addObject:polyline];
      _polys = polys;
      [self tick];
  }
  
  
  
  - (void)tick {
      //Create steps for polyline(dotted polylines)
      for (GMSPolyline *poly in _polys) {
          poly.spans =
          GMSStyleSpans(poly.path, _styles, _lengths, kGMSLengthGeodesic, _pos);
      }
      _pos -= _step;
  
      //Animate the polyline like moving from source to destination
      if (kAnimate) {
          __weak id weakSelf = self;
          dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 5),
                         dispatch_get_main_queue(),
                         ^{ [weakSelf tick]; });
      }
  }
  
  -(NSMutableArray *)decodePolyLine {
  
      NSMutableString *encoded = [[NSMutableString alloc] initWithCapacity:[self.strPolyPoints length]];
      [encoded appendString:self.strPolyPoints];
  
      [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
                                  options:NSLiteralSearch
                                    range:NSMakeRange(0, [encoded length])];
      NSInteger len = [encoded length];
      NSInteger index = 0;
      NSMutableArray *array = [[NSMutableArray alloc] init];
  
      NSInteger lat=0;
      NSInteger lng=0;
      while (index &lt; len) {
          NSInteger b;
          NSInteger shift = 0;
          NSInteger result = 0;
          do {
              b = [encoded characterAtIndex:index++] - 63;
              result |= (b &amp; 0x1f) &lt;&lt; shift;
              shift += 5;
          } while (b &gt;= 0x20);
          NSInteger dlat = ((result &amp; 1) ? ~(result &gt;&gt; 1) : (result &gt;&gt; 1));
          lat += dlat;
          shift = 0;
          result = 0;
          do {
              b = [encoded characterAtIndex:index++] - 63;
              result |= (b &amp; 0x1f) &lt;&lt; shift;
              shift += 5;
          } while (b &gt;= 0x20);
          NSInteger dlng = ((result &amp; 1) ? ~(result &gt;&gt; 1) : (result &gt;&gt; 1));
          lng += dlng;
          NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5];
          NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5];
          printf("[%f,", [latitude doubleValue]);
          printf("%f]", [longitude doubleValue]);
          CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
          [array addObject:loc];
  
      }
      return array;
  }
  </code></pre>