I am trying to draw the route between two points on the maps.
I am using dispatch_sync to update the UI but still the UI is not updating. It is not drawing the path. Can you please find the mistake because the code is working sometimes with breakpoints but sometimes it is not working.
- (IBAction)navigateActbtn:(id)sender {
NSString *str = [NSString stringWithFormat:@"%@origin=%f,%f&destination=%f,%f&key=%@",@"https://maps.googleapis.com/maps/api/directions/json?",
self.marker.position.latitude,
self.marker.position.longitude,
self.SecondMarker.position.latitude,
self.SecondMarker.position.longitude,
@"USEURAPIKEY"];
str = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:str];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:request
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
self.dataReceive = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
// handle response
if (!error && [data length]) {
dispatch_sync(dispatch_get_main_queue(), ^{
[self mainQueue];
});
}
else{
NSLog(@"%@",error.description);
}
}] resume];
}
-(void)mainQueue{
NSLog(@"main queue");
self.path1 =[GMSPath pathFromEncodedPath:self.dataReceive[@"routes"][0][@"overview_polyline"][@"points"]];
_singleLine = [GMSPolyline polylineWithPath:self.path1];
_singleLine.strokeWidth = 7;
_singleLine.strokeColor = [UIColor greenColor];
_singleLine.map = self.mapView;
}