0

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;

}
  • Use `async`, not `sync` – shallowThought Apr 25 '17 at 11:32
  • yes i have tried with async also not working. –  Apr 25 '17 at 11:33
  • 1
    use `async`. If you are on the main queue already, `sync` will cause a deadlock. If it does not work, check the data returned (`dataReceive`). – shallowThought Apr 25 '17 at 11:34
  • @shallowThought it is not working with sync as well as async. Yes, checked the data received also it is getting the response. –  Apr 25 '17 at 11:36
  • Please answer someone? –  Apr 25 '17 at 12:06
  • Please refer this one to draw the path http://stackoverflow.com/questions/22550849/drawing-route-between-two-places-on-gmsmapview-in-ios/22551085#22551085 i think you are doing wrong for create the draw path – Bhupat Bheda Apr 25 '17 at 13:00
  • @BhupatBheda that is not the correct way of doing routing the directions. It is temporarily making the straight line. I am updating it from the google API. –  Apr 25 '17 at 13:15
  • @HarjotSingh you got the all point so add those all points in an array then after you have to add all point in GMSMutablePath – Bhupat Bheda Apr 25 '17 at 13:17
  • @BhupatBheda thanks for ur comments. –  Apr 26 '17 at 04:01

0 Answers0