0

I want to draw a route from point A to point B on my mapView (GMSMapView).

I've tried to do it like this:

GMSMutablePath *path=[GMSMutablePath path];
    [path addCoordinate:self.locationManager.location.coordinate];
    [path addCoordinate:marker.position];

    GMSPolyline *rectangle=[GMSPolyline polylineWithPath:path];
    rectangle.strokeWidth=2.f;
    rectangle.map=self.mapView;

But it just drew a line that connects point A to point B but not the route itself (the directions through streets, roads etc...).

I want to draw on the map the best way to get to point B from point A by walking.

How can I do that?

Thank you!

FS.O
  • 403
  • 6
  • 24
  • Deep dive into Google Directions API (https://developers.google.com/maps/documentation/directions/intro#DirectionsRequests) for your answer. – Hima Mar 08 '16 at 09:13
  • @Hima Can you give me a code example for how can I do that? Thank you! – FS.O Mar 08 '16 at 09:21
  • They have already given Query about how you can call API of directions, Find "https://maps.googleapis.com/maps/api/directions/json?origin=sydney,au&destination=perth,au&waypoints=via:-37.81223%2C144.96254%7C-34.92788%2C138.60008&key=YOUR_API_KEY " in **Waypoints** topic in that page. – Hima Mar 08 '16 at 09:28
  • Check this http://stackoverflow.com/questions/22091271/googlemap-api-gives-wrong-coordinates-for-direction-between-two-points – Varinder Singh iPhone Dev Mar 14 '16 at 10:20

2 Answers2

0

Use google maps direction API. Send starting origin and destination location point to it using NSURLConnection or any other 3rd Party library like AFNetworking etc. Get Response in XML or JSON and parse it.

0

{ dispatch_async(dispatch_get_main_queue(), ^{

        PlayBackObject *displayobj=[[PlayBackObject alloc]init];

        displayobj=[playBackMutArray objectAtIndex:0];

        double distanceLatitude = [displayobj.latitudeStr doubleValue];
        double distanceLongtitude =[displayobj.longitudeStr doubleValue];

        CLLocationDegrees lat = distanceLatitude;
        CLLocationDegrees lon =distanceLongtitude;

        GMSCameraPosition* camera = [GMSCameraPosition
                                     cameraWithLatitude:lat
                                     longitude:lon
                                     zoom: 9];


        GMSMapView *ListAlertMultimap;



            ListAlertMultimap =[GMSMapView mapWithFrame: CGRectMake(0, 0, 414,736) camera: camera];
            [GmapPlayView addSubview:ListAlertMultimap];



          path = [GMSMutablePath path];

        for (int i = 0; i < [playBackMutArray count]-1; i++)

        {

            double lat = [[[playBackMutArray objectAtIndex:i]valueForKey:@"latitudeStr" ] doubleValue];
            double lng = [[[playBackMutArray objectAtIndex:i]valueForKey:@"longitudeStr" ] doubleValue];

            marker = [[GMSMarker alloc] init];

            marker.position = CLLocationCoordinate2DMake(lat,lng);

            marker.appearAnimation = kGMSMarkerAnimationPop;
            NSString *title=[[playBackMutArray objectAtIndex:i]valueForKey:@"trackeridStr"];
            NSString *speed=[[playBackMutArray objectAtIndex:i]valueForKey:@"speedStr"];
            NSString *direction=[[playBackMutArray objectAtIndex:i]valueForKey:@"directionStr"];

            marker.title=[NSString stringWithFormat:@"Tracker ID : %@",title];
            marker.snippet=[NSString stringWithFormat:@"Speed : %@km/hr Direction : %@",speed,direction];

 //         marker.icon = [UIImage imageNamed:@"marker_start"];

            [ListAlertMultimap setMinZoom:2 maxZoom:25];
            marker.map = ListAlertMultimap;

            [path addLatitude:lat longitude:lng];
        }
           NSLog(@"Direction path");
           polyline = [GMSPolyline polylineWithPath:path];
           polyline.strokeColor = [UIColor blueColor];
           polyline.strokeWidth = 5.f;
           polyline.map=ListAlertMultimap;

    });

}