1

I am using this source code to draw route but getting crash on line,

 polyline = [GMSPolyline polylineWithPath:path];

Terminating app due to uncaught exception 'GMSThreadException', reason: 'All calls to the Google Maps SDK for iOS must be made from the UI thread'

Even i gone through but not works for me.

Community
  • 1
  • 1

3 Answers3

0

When performing UI Updates in closures,Do remember to get main thread and perform UI Operations on main thread only.

Mistake what i did is,I'm trying to plot markers in web service completion block.

dispatch_async(dispatch_get_main_queue(),
{
    //Your code...
})
Sargis
  • 1,196
  • 1
  • 18
  • 31
  • Yes that was the mistake, i call that method on main thread but while take polyline from path there also need to do on main thread. –  Apr 20 '17 at 11:21
0
//Drawing Route Between Two Places on GMSMapView in iOS
- (void)drawRoute
{
CLLocation *myOrigin = [[CLLocation alloc]initWithLatitude:latitudeValue longitude:longitudeValue];
CLLocation *myDestination = [[CLLocation alloc] initWithLatitude:30.7333 longitude:76.7794];
[self fetchPolylineWithOrigin:myOrigin destination:myDestination completionHandler:^(GMSPolyline *polyline)
 {
     if(polyline)
         polyline.map = myMapView;
 }];
}

- (void)fetchPolylineWithOrigin:(CLLocation *)origin destination:(CLLocation *)destination completionHandler:(void (^)(GMSPolyline *))completionHandler{
    NSString *originString = [NSString stringWithFormat:@"%f,%f", origin.coordinate.latitude, origin.coordinate.longitude];
    NSString *destinationString = [NSString stringWithFormat:@"%f,%f", destination.coordinate.latitude, destination.coordinate.longitude];
    NSString *directionsAPI = @"https://maps.googleapis.com/maps/api/directions/json?";
    NSString *directionsUrlString = [NSString stringWithFormat:@"%@&origin=%@&destination=%@&mode=driving", directionsAPI, originString, destinationString];
    NSURL *directionsUrl = [NSURL URLWithString:directionsUrlString];
    NSURLSessionDataTask *fetchDirectionsTask = [[NSURLSession sharedSession] dataTaskWithURL:directionsUrl completionHandler:
                                                 ^(NSData *data, NSURLResponse *response, NSError *error)
                                                 {
                                                     NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
                                                     if(error)
                                                     {
                                                         if(completionHandler)
                                                             completionHandler(nil);
                                                         return;
                                                     }
                                                     dispatch_async(dispatch_get_main_queue(), ^{
                                                         //Your code...
                                                         NSArray *routesArray = [json objectForKey:@"routes"];
                                                         GMSPolyline *polyline = nil;
                                                         if ([routesArray count] > 0)
                                                         {
                                                             NSDictionary *routeDict = [routesArray objectAtIndex:0];
                                                             NSDictionary *routeOverviewPolyline = [routeDict objectForKey:@"overview_polyline"];
                                                             NSString *points = [routeOverviewPolyline objectForKey:@"points"];
                                                             GMSPath *path = [GMSPath pathFromEncodedPath:points];
                                                             polyline = [GMSPolyline polylineWithPath:path];
                                                             polyline.strokeWidth = 3.f;
                                                             polyline.strokeColor = [UIColor blueColor];
                                                             polyline.map = myGoogleMapView;
                                                             self.customMapViewRef = myGoogleMapView;
                                                         }
                                                     });
                                                 }];
    [fetchDirectionsTask resume];
}
Mannam Brahmam
  • 2,225
  • 2
  • 24
  • 36
0

Call your Method in Main thread.

Objective-C

dispatch_async(dispatch_get_main_queue(), ^{
    [self callMethod];
});

Swift 3 OnWards

DispatchQueue.main.async {
    self.callMethod()
}

Earlier Swift

dispatch_async(dispatch_get_main_queue()) {
    self.callMethod()
}
B.Saravana Kumar
  • 1,208
  • 6
  • 20
  • 48