2013-12-17 19 views
1
NSString *urlPath=[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=false",coordinate1.latitude,coordinate1.longitude,coordinate2.latitude,coordinate2.longitude]; 

//NSLog(@"the url for searching is : %@",urlPath); 
NSURL *url=[NSURL URLWithString:urlPath]; 
NSData *data=[NSData dataWithContentsOfURL:url]; 
NSDictionary *object = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
NSNumber *results = [[[[object valueForKey:@"routes"]valueForKey:@"legs"]valueForKey:@"distance"]valueForKey:@"value"]; 

NSLog(@"Count %@",results); 

結果出來的值:獲取無支架

(
    (
     12078 
    ) 
) 

是有可能只得到多少呢?

+0

你能在一個代碼塊再次發佈您的代碼,所以我們可以看了嗎? –

+3

你沒有得到一個數字,你得到一個數組數組。 – zneak

+0

[JSONKit](https://github.com/johnezang/JSONKit)可能會爲您節省一些時間。 – johnMa

回答

1

縱觀谷歌API的地圖我可以告訴大家,那路線包括腿和腳有距離屬性。

{ 
    "status": "OK", 
    "routes": [ { 
    "summary": "I-40 W", 
    "legs": [ { 
     "steps": [ { 
     "travel_mode": "DRIVING", 
     "start_location": { 
      "lat": 41.8507300, 
      "lng": -87.6512600 
     }, 
     "end_location": { 
      "lat": 41.8525800, 
      "lng": -87.6514100 
     }, 
     "polyline": { 
      "points": "[email protected]" 
     }, 
     "duration": { 
      "value": 19, 
      "text": "1 min" 
     }, 
     "html_instructions": "Head \u003cb\u003enorth\u003c/b\u003e on \u003cb\u003eS Morgan St\u003c/b\u003e toward \u003cb\u003eW Cermak Rd\u003c/b\u003e", 
     "distance": { 
      "value": 207, 
      "text": "0.1 mi" 
     } 
     }, 
     ... 
     ... additional steps of this leg 
    ... 
    ... additional legs of this route 
     "duration": { 
     "value": 74384, 
     "text": "20 hours 40 mins" 
     }, 
     "distance": { 
     "value": 2137146, 
     "text": "1,328 mi" 
     }, 
     "start_location": { 
     "lat": 35.4675602, 
     "lng": -97.5164276 
     }, 
     "end_location": { 
     "lat": 34.0522342, 
     "lng": -118.2436849 
     }, 
     "start_address": "Oklahoma City, OK, USA", 
     "end_address": "Los Angeles, CA, USA" 
    } ], 

所以對於第一路線的第一站的距離應該是:

NSNumber *distance = object[@"routes"][0][@"legs"][0][@"distance"][@"value"]; 
+0

非常感謝您的配偶。這是一個很大的幫助 – user2898390

0

不要將它保存爲NSNumber,因爲它是一個字符串值。
使用NSNumberFormatterNSString另存爲NSNumber

 
NSNumberFormatter *f = [[NSNumberFormatter alloc] init]; 
[f setNumberStyle:NSNumberFormatterDecimalStyle]; 
NSNumber *results = [f numberFromString:[[[[object valueForKey:@"routes"] valueForKey:@"legs"] valueForKey:@"distance"] valueForKey:@"value"]]; 
0
NSString *urlPath = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=12.9715,77.045&destination=12.7152,77.888&sensor=false"]; 

//NSLog(@"the url for searching is : %@", urlPath); 
NSURL *url = [NSURL URLWithString:urlPath]; 
NSData *data = [NSData dataWithContentsOfURL:url]; 
NSDictionary *object = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
NSArray *results = [[[[object valueForKey:@"routes"] valueForKey:@"legs"] valueForKey:@"distance"] valueForKey:@"value"]; 

NSLog(@"Count %@", results); 
NSLog(@"Count %@", results[0][0]);