-2

I am trying the draw path line on the map between two location.it is not showing on the road it's like the shortest distance between two place mark on the road.

I have tried with snap on road . I MKDirectionsRequestas well as (it is giving error , I search then some places it is showing this is not working in India.)

Any other way to draw the road line.

Abhishek Mishra
  • 1,625
  • 16
  • 32

2 Answers2

0

You can use googleMAP API for this.

- (void)drawRoute :(CLLocationCoordinate2D)myOrigin destination:(CLLocationCoordinate2D)myDestination
 {
       [self fetchPolylineWithOrigin:myOrigin destination:myDestination completionHandler:^(GMSPolyline *polyline1)
      {
         if(polyline1)
             polyline1.map = mapView_;
      }];
  }


- (void)fetchPolylineWithOrigin:(CLLocationCoordinate2D )origin destination:(CLLocationCoordinate2D)destination completionHandler:(void (^)(GMSPolyline *))completionHandler
{
NSString *originString = [NSString stringWithFormat:@"%f,%f", origin.latitude, origin.longitude];
NSString *destinationString = [NSString stringWithFormat:@"%f,%f", destination.latitude, destination.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)
                                             {
                                                 dispatch_async(dispatch_get_main_queue(), ^{
                                                     NSError *error;
                                                 NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
                                                 if(error)
                                                 {
                                                     if(completionHandler)
                                                         completionHandler(nil);
                                                     return;
                                                 }

                                                 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.strokeColor = [UIColor redColor];
                                                     polyline.strokeWidth = 5.0;
                                                 }

                                                 if(completionHandler)
                                                     completionHandler(polyline);
                                             });
                                             }];
 [fetchDirectionsTask resume];
}
Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51
0

Satellite, Direction, Navigation services of Apple are not present in several Countries thats why you are not getting the appropriate results.

Please check this link from Apple: http://www.apple.com/in/ios/feature-availability/

Is better you use Google maps in your app to get your location coordinates.

https://developers.google.com/maps/documentation/ios-sdk/start this link will be helpful to you

Abhi
  • 243
  • 4
  • 19