2017-04-25 26 views
0

Google Directions APIの回答を取得しようとしています。これでエラーを見つけていただけますか?方向APIのGoogleマップからの応答を取得するにはどうすればよいですか?

NSString *str = [NSString stringWithFormat:@"%@origin=%f,%f&destination=%f,%f&key=%@",@"https://maps.googleapis.com/maps/api/directions/json?", 
        self.marker.position.latitude, 
        self.marker.position.longitude, 
        self.SecondMarker.position.latitude, 
        self.SecondMarker.position.longitude, 
        @"AIzaSyDhZvxehd6ZDKFOB67WIyeLpy7KITwRPw0"]; 
    //str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    str = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]; 
    NSURL *url = [NSURL URLWithString:str]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
    //NSString *params = @""; 
    [request setHTTPMethod:@"POST"]; 
    //[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]]; 

    NSURLSession *session = [NSURLSession sharedSession]; 
    [[session dataTaskWithRequest:request 
       completionHandler:^(NSData *data, 
            NSURLResponse *response, 
            NSError *error) { 
        // handle response 
        if (!error && [data length]) { 
         NSLog(@"Reponse: %@",response); 
        } 
        else{ 
         NSLog(@"%@",error.description); 
        } 
       }] resume]; 

コードの一部を入力してください。

エラーメッセージ:

Reponse: <NSHTTPURLResponse: 0x60800002d480> { URL: https://maps.googleapis.com/maps/api/directions/json?origin=-33.860000,151.200000&destination=-34.224142,150.068408&key=AIzaSyDhZvxehd6ZDKFOB67WIyeLpy7KITwRPw0 

} {ステータスコード:200、ヘッダ{ "のCache-Control" = "公共、MAX-年齢= 86400"。 "Content-Encoding" = gzip; "Content-Length" = 23856; "Content-Type" = "application/json; charset = UTF-8"; 日付= "火曜日、25 Apr 2017 08:48:36 GMT"; Expires = "Wed、26 Apr 2017 08:48:36 GMT"; サーバー= mafe。 Vary = "Accept-Language"; "alt-svc" = "quic = \":443 \ "; ma = 2592000; v = \" 37,36,35 \ ""; "x-frame-options" = SAMEORIGIN; "x-xss-protection" = "1; mode = block"; }}

+0

のように試してみてください。 – KKRocks

+0

編集を確認してください –

+0

@HarjotSinghPanesar今度はデータを取得してシリアル化されたJSONにアクセスする必要がありますが、エラーは発生しません –

答えて

0

あなたのエラーメッセージを追加し、この

NSString *str = [NSString stringWithFormat:@"%@origin=%f,%f&destination=%f,%f&key=%@",@"https://maps.googleapis.com/maps/api/directions/json?", 
        self.marker.position.latitude, 
        self.marker.position.longitude, 
        self.SecondMarker.position.latitude, 
        self.SecondMarker.position.longitude, 
        @"AIzaSyDhZvxehd6ZDKFOB67WIyeLpy7KITwRPw0"]; 

str = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]; 
NSURL *url = [NSURL URLWithString:str]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 

[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request setHTTPMethod:@"POST"]; 
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil]; 
[[session dataTaskWithRequest:request completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) { 

       if (!error && [data length]) 
       { 
        NSLog(@"Reponse: %@",response); 

        NSError *e = nil; 
        NSDictionary *JSONarray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error:&e]; 
        NSLog(@"Response dictionary is%@",JSONarray); 
       } 
       else 
       { 
        NSLog(@"%@",error.description); 
       } 
      }] resume]; 
関連する問題