MatrixDistance API from Google for iOS



我正在尝试构建一个带有地图的应用程序,用户可以在地图中选择他的原籍地址和目的地地址。。一切都很好,但我无法访问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;

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

我认为您的调用已被视为无效的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"];

最新更新