Google Maps iOS SDK, Getting Directions between 2 locations

前端 未结 11 1292
栀梦
栀梦 2020-12-02 07:52

While I am using Google Maps SDK, I am trying to get driving direction between two locations on iOS. I know we can do this using two methods:-

1.) Using URL Scheme,

11条回答
  •  执笔经年
    2020-12-02 08:33

        NSString *urlString = [NSString stringWithFormat:
                           @"%@?origin=%f,%f&destination=%f,%f&sensor=true&key=%@",
                           @"https://maps.googleapis.com/maps/api/directions/json",
                           mapView.myLocation.coordinate.latitude,
                           mapView.myLocation.coordinate.longitude,
                           destLatitude,
                           destLongitude,
                           @"Your Google Api Key String"];
    NSURL *directionsURL = [NSURL URLWithString:urlString];
    
    
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:directionsURL];
    [request startSynchronous];
    NSError *error = [request error];
    if (!error) {
        NSString *response = [request responseString];
        NSLog(@"%@",response);
        NSDictionary *json =[NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingMutableContainers error:&error];
        GMSPath *path =[GMSPath pathFromEncodedPath:json[@"routes"][0][@"overview_polyline"][@"points"]];
        GMSPolyline *singleLine = [GMSPolyline polylineWithPath:path];
        singleLine.strokeWidth = 7;
        singleLine.strokeColor = [UIColor greenColor];
        singleLine.map = self.mapView;
    }
    else NSLog(@"%@",[request error]);
    

    Note: make Sure Your Google Direction API Sdk Is Enable in Your google developer Console.

提交回复
热议问题