Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing Route Between Two Places on GMSMapView in iOS

I am Developing an iOS Application. In that Application i am having 2 Fields From and To. I Entered Address using Google Auto Complete API.and also i am able to Getting the Latitude and Longitude of the 2 places and able to show markers on the GMSMapView.

Now i Want to Draw Route Between these 2 Places. I found a solution when we use MKMapView. But i was Unable to find the solution for GMSMapView. please help me to Draw the route between these 2 points in GMSMapView.

If possible please give me some important links for this.

Thanks.

like image 420
Suresh Peddisetti Avatar asked Mar 21 '14 05:03

Suresh Peddisetti


People also ask

Can you draw a route on Google Maps iOS?

You can trace a path or highlight an area on your map by drawing lines and shapes.

Is there an app where I can draw my route?

Footpath is the ultimate companion for planning and navigating custom routes. Join millions of adventurers and plan out your perfect route. Quickly measure distances by tracing a map with your finger or Apple Pencil.


Video Answer


2 Answers

`first get all points coordinates which are coming in route then add these points latitude and longitude in path in will draw path according to that`   GMSCameraPosition *cameraPosition=[GMSCameraPosition cameraWithLatitude:18.5203 longitude:73.8567 zoom:12]; _mapView =[GMSMapView mapWithFrame:CGRectZero camera:cameraPosition]; _mapView.myLocationEnabled=YES; GMSMarker *marker=[[GMSMarker alloc]init]; marker.position=CLLocationCoordinate2DMake(18.5203, 73.8567); marker.icon=[UIImage imageNamed:@"aaa.png"] ; marker.groundAnchor=CGPointMake(0.5,0.5); marker.map=_mapView; GMSMutablePath *path = [GMSMutablePath path];    [path addCoordinate:CLLocationCoordinate2DMake(@(18.520).doubleValue,@(73.856).doubleValue)]; [path addCoordinate:CLLocationCoordinate2DMake(@(16.7).doubleValue,@(73.8567).doubleValue)];  GMSPolyline *rectangle = [GMSPolyline polylineWithPath:path]; rectangle.strokeWidth = 2.f; rectangle.map = _mapView; self.view=_mapView; 
like image 70
johny kumar Avatar answered Sep 21 '22 20:09

johny kumar


I've written the following code which should do the trick for you:

- (void)drawRoute {     [self fetchPolylineWithOrigin:myOrigin destination:myDestination completionHandler:^(GMSPolyline *polyline)      {          if(polyline)              polyline.map = self.myMap;      }]; }  - (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;              }               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];              }               // run completionHandler on main thread                                                         dispatch_sync(dispatch_get_main_queue(), ^{                  if(completionHandler)                       completionHandler(polyline);              });          }];     [fetchDirectionsTask resume]; } 
like image 27
Tarek Avatar answered Sep 19 '22 20:09

Tarek