来自Google for iOS的MatrixDistance API

问题描述:

我正在尝试构建一个应用程序,其中用户可以选择他的原始地址和目标地址。它一切正常,但无法访问Google API距离矩阵。 。来自Google for iOS的MatrixDistance 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; 

,但没有运气,任何想法如何解决这个问题?

+1

您可以使用此也用于查找距离b/w的两个定位://获取CLLocation FOT两个地址 CLLocation *位置= [[CLLocation页头] initWithLatitude:address.latitude经度:address.longitude]。 //计算它们之间的距离 CLLocationDistance distance = [firstLocation distanceFromLocation:secondLocation]; – 2012-03-19 07:00:45

+0

我试过这个,但是[firstLocation distanceFromLocation:secondLocation]是根据空中距离,而不是道路。 – dsafa 2012-03-19 10:12:29

我认为你的电话被认为是无效的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"]; 
+0

我的代码和你的代码有什么区别?你改变了什么? – dsafa 2012-03-19 10:13:03

+0

它适合你吗?让我知道,我会告诉你你和我的代码之间的区别。 – Kuldeep 2012-03-19 10:14:57

+0

将此行与您的相比较:@“/ maps/api/distancematrix/xml?originins =%@&destinations =%@&mode = driving&language = zh-CN&sensor = false” – Kuldeep 2012-03-19 10:29:34