0

我正在尝试使用地图构建一个应用程序,用户可以在其中选择他的原始地址和目标地址。一切正常,但我无法访问 Google API 距离矩阵。

我正在尝试以下操作:

NSString *urlPath = [NSString stringWithFormat:@"/maps/api/distancematrix/xml?origins=%@=%@&mode=driving&language=en-EN&sensor=false" ,polazisteField.text , odredisteField.text];
NSURL *url = [[NSURL alloc]initWithScheme:@"http" host:@"maps.googleapis.com" path:urlPath];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc]init]autorelease];
[request setURL:url];
[request setHTTPMethod:@"GET"];

NSURLResponse *response ;
NSError *error;
NSData *data;
data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
address.text = result;

,但没有运气,知道如何解决这个问题吗?

4

1 回答 1

0

我认为您的调用已被视为无效的 API 调用。

尝试以下从您的代码片段编辑的代码。

 NSString *urlPath = [NSString stringWithFormat:@"/maps/api/distancematrix/json?origins=%@&destinations=%@&mode=driving&language=en-EN&sensor=false" ,polazisteField.text , odredisteField.text];
NSURL *url = [[NSURL alloc]initWithScheme:@"http" host:@"maps.googleapis.com" path:urlPath];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL:url];
[request setHTTPMethod:@"GET"];

NSURLResponse *response ;
NSError *error;
NSData *data;
data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSMutableDictionary *jsonDict= (NSMutableDictionary*)[NSJSONSerialization  JSONObjectWithData:data options:kNilOptions error:&error];
NSMutableDictionary *newdict=[jsonDict valueForKey:@"rows"];
NSArray *elementsArr=[newdict valueForKey:@"elements"];
NSArray *arr=[elementsArr objectAtIndex:0];
NSDictionary *dict=[arr objectAtIndex:0];
NSMutableDictionary *distanceDict=[dict valueForKey:@"distance"];
NSLog(@"distance:%@",[distanceDict valueForKey:@"text"]);

   NSString *result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
   address.text = [distanceDict valueForKey:@"text"];
于 2012-03-19T06:28:14.793 回答