update marker position in Google Maps in IOS app

可紊 提交于 2019-12-12 03:54:52

问题


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)


回答1:


And here is the solution:What I needed was to update the position of marker only but I was updating the marker position and then sending that position to the server again.So I corrected that and it worked perfectly.Here is the code for that:

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

And here is the selector method:

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

Earlier I was including all the code in the selector method.



来源:https://stackoverflow.com/questions/33013481/update-marker-position-in-google-maps-in-ios-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!