0
我正在嘗試獲取Google Directions API的回覆。你能找到這個錯誤嗎?如何從API Google Maps地圖獲取響應?
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,標頭{ 「緩存控制」= 「公衆,最大年齡= 86400」; 「內容編碼」= gzip; 「Content-Length」= 23856; 「Content-Type」=「application/json; charset = UTF-8」; 日期=「2017年4月25日星期二08:48:36 GMT」; Expires =「2017年4月26日星期三08:48:36 GMT」; Server = mafe; Vary =「Accept-Language」; 「alt-svc」=「quic = \」:443 \「; ma = 2592000; v = \」37,36,35 \「」; 「x-frame-options」= SAMEORIGIN; 「x-xss-protection」=「1; mode = block」; }}
添加您的錯誤信息。 – KKRocks
檢查編輯請 –
@HarjotSinghPanesar你沒有得到錯誤,而不是你正在得到響應,現在你需要訪問數據和序列化的JSON從它 –