0

How can I draw custom routes between two destinations which is not connecting with a road?

For example, if the Source is The Pizza Factory and the Destination is ATI Solutions I have to draw route like this but there is not roads to connect these two place.

enter image description here

I tried Google's Polylines code but it provides waypoints on the roads only.

NSString *urlString = [NSString stringWithFormat:@"%@?origin=%@,%@&destination=%f,%f&sensor=false&waypoints=optimize:true&mode=driving", @"https://maps.googleapis.com/maps/api/directions/json", getmycurrlat, getmycurrlong, LATI, LONGI];
    NSLog(@"my driving api URL --- %@", urlString);
    NSLog(@"you clicked on button %ld", (long)sender.tag);
    NSURL* url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@", urlString]];
    NSURLResponse* res;
    NSError* err;
    NSData* data = [NSURLConnection sendSynchronousRequest:[[NSURLRequest alloc] initWithURL:url] returningResponse:&res error:&err];
    if (data == nil) {
        return;
    }

    NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSDictionary* routes = [dic objectForKey:@"routes"][0];
    NSDictionary* route = [routes objectForKey:@"overview_polyline"];
    NSString* overview_route = [route objectForKey:@"points"];

    GMSPath* path = [GMSPath pathFromEncodedPath:overview_route];
    GMSPolyline* polyline = [GMSPolyline polylineWithPath:path];
    polyline.strokeWidth = 5.f;
    polyline.map = self.mapView;

All helps are appreciated!!

7
  • map preferred standard way to make routs is on roads with appropriate space to pass away, what kind of custom route you want to make ?
    – vaibhav
    Jan 23, 2017 at 7:58
  • There is no road connecting between pizza factory and ATI solution only there is a space between two buildings. Is there any possible way to draw line something like in snapshot? Jan 23, 2017 at 8:01
  • in my exp map will only create route on connected roads if you provide static lat long prior but there is dynamic way also to create routs bet source to dest while moving have a look.
    – vaibhav
    Jan 23, 2017 at 8:08
  • Is there any way to create custom waypoints? Jan 23, 2017 at 8:10
  • Did you have any luck with this?
    – FabKremer
    May 23, 2017 at 19:41

1 Answer 1

0

Try this is works for me, hope it helps you, i am using AFNetworking to get data

    NSString *url1 = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?&origin=%@&destination=%@&mode=driving",[NSString stringWithFormat:@"%f,%f",self.locationManager.location.coordinate.latitude,self.locationManager.location.coordinate.longitude],pickLoc];
    AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
    [operationManager POST:url1
                parameters: nil
                   success:^(AFHTTPRequestOperation *operation, id responseObject) {
                       // NSLog(@"JSON: %@", [responseObject description]);

                       NSArray *routesArray = [responseObject 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;
                           polyline.strokeColor = [UIColor colorWithRed:0.9607 green:0.0358 blue:0.1529 alpha:0.6];
                           polyline.map = self.viewMapView;
                       }
                   }
                   failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                       NSLog(@"Error: %@", [error description]);
                   }
     ];
3
  • This will draw polyline on roads. But I need to draw polyline on non road areas. Jan 24, 2017 at 5:19
  • you can see the routsArray and set your own custom lat long as per your route and make the polyline Jan 24, 2017 at 13:36
  • route array will provide only the waypoints of roads. But I need waypoints of non-roads. Please check the above image. Jan 24, 2017 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.