更新IOS地图中Google地图的标记位置

我需要更新绘制的路线上的标记位置,直到它到达目的地。现在这里是访问服务器的代码:

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

在这之后,我只是更新标记超时的位置,我计算了新的位置。问题出现在给定的路线上,我如何获得标记的新位置。我从儿子数据中得到它,上面的URL.So这里是获取我的新位置的纬度和经度的代码。

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

所有这些代码都在NSTimer类的select器方法中。这里是完整的代码:

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

请注意,我在这里使用AFNetworking。现在的问题是,我的标记随机更新,我的代码崩溃。任何想法是什么问题?错误是这样的:

  Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (7) beyond bounds (7)'. 

请帮助我们(关注那些在开始时分别提到的4个陈述),这就是代码被搞乱的地方)

这里是解决scheme:我需要的是只更新标记的位置,但我正在更新标记的位置,然后再次发送到服务器的位置。所以我纠正它,它的工作完美。这里是代码为:

  -(void)GETRequest{ NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(callInOne:) userInfo:nil repeats:YES]; 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]; operation = [[AFHTTPRequestOperation alloc]initWithRequest:urlRequest]; operation.responseSerializer = [AFJSONResponseSerializer serializer]; __weak ViewController *_self = self; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,id responseObject){ myResponseObject = 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_; } failure:^(AFHTTPRequestOperation *operation,id responseObject){ NSLog(@"failure"); }]; [operation start]; } 

这里是select器方法:

  -(void)callInOne:(NSTimer*)timer{ if(i<=30){ NSString *latitudeInString = myResponseObject[@"routes"][0][@"legs"][0][@"steps"][i][@"end_location"][@"lat"]; CLLocationDegrees latitudeInDegrees = [latitudeInString doubleValue]; NSString *longitudeInString = myResponseObject[@"routes"][0][@"legs"][0][@"steps"][i][@"end_location"][@"lng"]; CLLocationDegrees longitudeInDegrees = [longitudeInString doubleValue]; marker.position = CLLocationCoordinate2DMake(latitudeInDegrees, longitudeInDegrees); } else if(i>31){ NSString *latitudeInString = myResponseObject[@"routes"][0][@"legs"][1][@"steps"][i][@"end_location"][@"lat"]; CLLocationDegrees latitudeInDegrees = [latitudeInString doubleValue]; NSString *longitudeInString = myResponseObject[@"routes"][0][@"legs"][1][@"steps"][i][@"end_location"][@"lng"]; CLLocationDegrees longitudeInDegrees = [longitudeInString doubleValue]; marker.position = CLLocationCoordinate2DMake(latitudeInDegrees, longitudeInDegrees); } i++; } 

之前我在select器方法中包含了所有的代码。