2012-03-18 42 views
0

我正在嘗試構建一個應用程序,其中用戶可以選擇他的原始地址和目標地址。它一切正常,但無法訪問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

回答

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"]; 
+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