I need to update the marker position on the drawn route till it reaches its destination.Now here is the code for accessing server:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=-32.90,151.80&waypoints=-33.30,151.50&alternatives=true",marker.position.latitude,marker.position.longitude]];
After this I am just updating the position of marker overtime I calculate the new position.The question arises how I am getting the new position for my marker on the given route.I am getting it from the son data that I'm getting from the above URL.So here is the code for getting the latitude and longitude for my new position.
NSString *latitudeInString = responseObject[@"routes"][0][@"legs"][0][@"steps"][i][@"end_location"][@"lat"]; CLLocationDegrees latitudeInDegrees = [latitudeInString doubleValue]; NSString *longitudeInString = responseObject[@"routes"][0][@"legs"][0][@"steps"][i][@"end_location"][@"lng"]; CLLocationDegrees longitudeInDegrees = [longitudeInString doubleValue];
All this code is inside the selector method of NSTimer class.Here is complete code:
-(void)callInOne:(NSTimer*)timer{ NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=-32.90,151.80&waypoints=-33.30,151.50&alternatives=true",marker.position.latitude,marker.position.longitude]]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:urlRequest]; operation.responseSerializer = [AFJSONResponseSerializer serializer]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,id responseObject){ NSString *polyLinePath = responseObject[@"routes"][0][@"overview_polyline"][@"points"]; GMSPath *path = [GMSPath pathFromEncodedPath:polyLinePath]; GMSPolyline *line = [GMSPolyline polylineWithPath:path]; line.spans = @[[GMSStyleSpan spanWithColor:[UIColor blackColor] segments:0.75]]; line.strokeWidth = 2; line.map = mapView_; self.view = mapView_; NSString *latitudeInString = responseObject[@"routes"][0][@"legs"][0][@"steps"][i][@"end_location"][@"lat"]; CLLocationDegrees latitudeInDegrees = [latitudeInString doubleValue]; NSString *longitudeInString = responseObject[@"routes"][0][@"legs"][0][@"steps"][i][@"end_location"][@"lng"]; CLLocationDegrees longitudeInDegrees = [longitudeInString doubleValue]; marker.position = CLLocationCoordinate2DMake(latitudeInDegrees, longitudeInDegrees); NSLog(@"%d",i); i++; } failure:^(AFHTTPRequestOperation *operation,id responseObject){ NSLog(@"failure"); }]; [operation start]; }
Note that I'm using AFNetworking here.Now the problem is that my marker is getting updated randomly and my code crashes.Any idea what is the problem?The error is like:
Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (7) beyond bounds (7)'.
Please help me out guys.(focus on those 4 statements mentioned separately at the beginning.That is where the code is getting screwed)