2012-03-18 11 views
0

ユーザーが自分の起点アドレスと宛先アドレスを選択するマップを使ってアプリを構築しようとしています。すべて正常に動作しますが、Google API距離行列にはアクセスできません。 。Googleの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

。 //それらの間の距離を計算する CLLocationDistance distance = [firstLocation distanceFromLocation:secondLocation]; –

+0

私はこれで試してみましたが、[firstLocation distanceFromLocation:secondLocation]は道路ではなく空気の距離に基づいています。 – dsafa

答えて

0

あなたの呼び出しは無効なAPI呼び出しとみなされていると思います。

あなたのコードスニペットから編集された以下のコードを試してください。 //両方のアドレスFOT CLLocationをCLLocationを取得*位置= [[CLLocation ALLOC] initWithLatitude:address.latitude経​​度:address.longitude]次の2つの位置の距離W bを/見つけるためにもこれを使用することができ

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

+0

それはあなたのために働いたのですか?あなたと私のコードの違いを教えてくれるよりも、私に教えてください。 – Kuldeep

+0

この行とあなたとの比較:@ "/ maps/api/distancematrix/xml?originins =%@&destination =%@&モード=運転&言語= en-EN&sensor = false" – Kuldeep