Reckoner
Reckoner

Reputation: 1041

Directions on Google Maps in IOS

I have started reading about Google maps and there are some things I have been able to implement since last 3 days. Besides this I had to read about basics of web services as well, so I have read about using AFNetworking library. Now the things that I have been able to implement are:

Here is the code for this implementation:

//Showing Map         
{
    GMSCameraPosition *cameraPosition = [GMSCameraPosition 
        cameraWithLatitude:-33.86 longitude:151.20 zoom:6];
    mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:cameraPosition];
    mapView_.myLocationEnabled = YES;
    mapView_.mapType = kGMSTypeNormal;
    mapView_.settings.myLocationButton = YES;

    mapView_.delegate = self;
    self.view = mapView_;

    //Showing marker    
    GMSMarker *marker = [GMSMarker new];
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
    marker.title = @"Sydney";
    marker.snippet = @"Australia";
    marker.map = mapView_;
    [self GETRequest];//method to implement google maps api
}

Here is the implementation of my method 'GETRequest':

-(void)GETRequest{

    NSURL  *url = [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/directions/json?origin=-33.86,151.20&destination=-32.90,151.80&waypoints=-33.30,151.50"];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:urlRequest];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,id responseObject){

        NSLog(@"success");
        NSLog(@"%@",responseObject);
        GMSMutablePath *path = [GMSMutablePath path];
        [path addCoordinate:CLLocationCoordinate2DMake(-33.86, 151.20)];
        [path addCoordinate:CLLocationCoordinate2DMake(-25.30, 140.50)];
        [path addCoordinate:CLLocationCoordinate2DMake(-32.90, 151.80)];

        GMSPolyline *line = [GMSPolyline polylineWithPath:path];
        line.strokeWidth = 2;
        line.map = mapView_;
        self.view = mapView_;
} failure

:^(AFHTTPRequestOperation *operation,id responseObject){


    NSLog(@"failure");

}];

[operation start];

Here is a screenshot of my output:

Now my question is: Are the routes always drawn like these straight lines? I want to see the specific directions but google API for directions documentation doesn't provide any code or explanation regarding that.

Upvotes: 1

Views: 230

Answers (1)

Jeff Wolski
Jeff Wolski

Reputation: 6382

The path in your screen shot is actually being drawn explicitly by this code:

    [path addCoordinate:CLLocationCoordinate2DMake(-33.86, 151.20)];
    [path addCoordinate:CLLocationCoordinate2DMake(-25.30, 140.50)];
    [path addCoordinate:CLLocationCoordinate2DMake(-32.90, 151.80)];

The call you are making returns a JSON string. You should parse all the coordinates (about 110 of them in this case) from there and draw the path using the parsed coordinates.

You can open your google url in a browser to see the data as well. https://maps.googleapis.com/maps/api/directions/json?origin=-33.86,151.20&destination=-32.90,151.80&waypoints=-33.30,151.50

Upvotes: 2

Related Questions