2016-02-23 70 views
0

也許有人可以提供幫助。我正在嘗試在下面的網址中獲取Google地圖位置和緯度的位置名稱。正如你可以從JSON看到的那樣,它首先打開結果,然後是address_components。我試圖在JSON中獲取第一個long_name和short_name,但是下面的代碼只給出了JSON中的最後一個long_name和short_name。NSDictionary沒有獲得第一項JSON

也許有人有某種想法,我在這個階段嘗試了一切,似乎沒有什麼能解決問題。

感謝,

CurtisB

-(void)getPlaceName 
    { 
     NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",53.348623,-6.243089]; 
     NSURL *url = [NSURL URLWithString:urlString]; 

     //Next we need a NSURLSession instance 
     NSURLSession *session = [NSURLSession sharedSession]; 

     //All tasks (there are three, see documentation) are created from an NSURLSession instance 
     //We want a DataTask 
     NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
      //This "block" (of code) will be executed when the call is complete 

      //Serialize the JSON to Foundation objects 
      NSDictionary *geocodeDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 

      //Parse through the JSON to get to where we want, address_components array 
      NSArray *resultsArray = geocodeDictionary[@"results"]; 
      NSDictionary *resultsDictionary = resultsArray[1]; 
      NSArray *addressComponents = resultsDictionary[@"address_components"]; 



      //Declare variables to hold desired results 
      NSString *longName; 
      NSString *shortName; 

      //The address_components array contains many dictionaries, 
      //we loop through each dictionary and check the types array 
      for (NSDictionary *addressComponentDictionary in addressComponents) { 

        longName = addressComponentDictionary[@"long_name"]; 
        shortName = addressComponentDictionary[@"short_name"]; 

      } 

      //Test log to see we are correct 
      NSLog(@"Long name for GetAddress is: %@", longName); 
      NSLog(@"Short Name is: %@", shortName); 
     }]; 

     [dataTask resume]; 
    } 

JSON LOG:

[8948:3198407] { 
    results =  (
       { 
      "address_components" =    (
           { 
        "long_name" = "National College of Ireland"; 
        "short_name" = "National College of Ireland"; 
        types =      (
         premise 
        ); 
       }, 
           { 
        "long_name" = "Mayor Street Lower"; 
        "short_name" = "Mayor Street Lower"; 
        types =      (
         route 
        ); 
       }, 
           { 
        "long_name" = "International Financial Services Centre"; 
        "short_name" = "International Financial Services Centre"; 
        types =      (
         "sublocality_level_1", 
         sublocality, 
         political 
        ); 
       }, 
           { 
        "long_name" = Dublin; 
        "short_name" = Dublin; 
        types =      (
         locality, 
         political 
        ); 
       }, 
           { 
        "long_name" = "Dublin 1"; 
        "short_name" = "Dublin 1"; 
        types =      (
         "postal_town" 
        ); 
       }, 
           { 
        "long_name" = "Dublin City"; 
        "short_name" = "Dublin City"; 
        types =      (
         "administrative_area_level_2", 
         political 
        ); 
       }, 
           { 
        "long_name" = Dublin; 
        "short_name" = Dublin; 
        types =      (
         "administrative_area_level_1", 
         political 
        ); 
       }, 
           { 
        "long_name" = Ireland; 
        "short_name" = IE; 
        types =      (
         country, 
         political 
        ); 
       } 
      ); 
      "formatted_address" = "National College of Ireland, Mayor Street Lower, International Financial Services Centre, Dublin 1, Ireland"; 
      geometry =    { 
       bounds =     { 
        northeast =      { 
         lat = "53.34914449999999"; 
         lng = "-6.2420244"; 
        }; 
        southwest =      { 
         lat = "53.3484171"; 
         lng = "-6.2435218"; 
        }; 
       }; 
       location =     { 
        lat = "53.3487808"; 
        lng = "-6.242773100000001"; 
       }; 
       "location_type" = ROOFTOP; 
       viewport =     { 
        northeast =      { 
         lat = "53.3501297802915"; 
         lng = "-6.241424119708499"; 
        }; 
        southwest =      { 
         lat = "53.3474318197085"; 
         lng = "-6.244122080291502"; 
        }; 
       }; 
      }; 
      "place_id" = "ChIJ7fLaG40OZ0gRJsCRPZA1_iA"; 
      types =    (
       premise 
      ); 
     }, 
       { 
      "address_components" =    (
           { 
        "long_name" = "Excise Walk"; 
        "short_name" = "Excise Walk"; 
        types =      (
         route 
        ); 
       }, 
           { 
        "long_name" = Dublin; 
        "short_name" = Dublin; 
        types =      (
         locality, 
         political 
        ); 
       }, 
           { 
        "long_name" = "Dublin City"; 
        "short_name" = "Dublin City"; 
        types =      (
         "administrative_area_level_2", 
         political 
        ); 
       }, 
           { 
        "long_name" = Dublin; 
        "short_name" = Dublin; 
        types =      (
         "administrative_area_level_1", 
         political 
        ); 
       }, 
           { 
        "long_name" = Ireland; 
        "short_name" = IE; 
        types =      (
         country, 
         political 
        ); 
       } 
      ); 
      "formatted_address" = "Excise Walk, Dublin, Ireland"; 
      geometry =    { 
       bounds =     { 
        northeast =      { 
         lat = "53.3492304"; 
         lng = "-6.2434942"; 
        }; 
        southwest =      { 
         lat = "53.3484317"; 
         lng = "-6.243662899999999"; 
        }; 
       }; 
       location =     { 
        lat = "53.34883110000001"; 
        lng = "-6.243578599999999"; 
       }; 
       "location_type" = "GEOMETRIC_CENTER"; 
       viewport =     { 
        northeast =      { 
         lat = "53.3501800302915"; 
         lng = "-6.242229569708497"; 
        }; 
        southwest =      { 
         lat = "53.3474820697085"; 
         lng = "-6.244927530291502"; 
        }; 
       }; 
      }; 
      "place_id" = "ChIJG_rIHI0OZ0gRHTz7pFf9r7o"; 
      types =    (
       route 
      ); 
     }, 
       { 
      "address_components" =    (
           { 
        "long_name" = "North Dock"; 
        "short_name" = "North Dock"; 
        types =      (
         neighborhood, 
         political 
        ); 
       }, 
           { 
        "long_name" = Dublin; 
        "short_name" = Dublin; 
        types =      (
         locality, 
         political 
        ); 
       }, 
           { 
        "long_name" = "Dublin City"; 
        "short_name" = "Dublin City"; 
        types =      (
         "administrative_area_level_2", 
         political 
        ); 
       }, 
           { 
        "long_name" = Dublin; 
        "short_name" = Dublin; 
        types =      (
         "administrative_area_level_1", 
         political 
        ); 
       }, 
           { 
        "long_name" = Ireland; 
        "short_name" = IE; 
        types =      (
         country, 
         political 
        ); 
       } 
      ); 
      "formatted_address" = "North Dock, Dublin, Ireland"; 
      geometry =    { 
       bounds =     { 
        northeast =      { 
         lat = "53.3608191"; 
         lng = "-6.1894282"; 
        }; 
        southwest =      { 
         lat = "53.34522"; 
         lng = "-6.2549399"; 
        }; 
       }; 
       location =     { 
        lat = "53.3497493"; 
        lng = "-6.2306567"; 
       }; 
       "location_type" = APPROXIMATE; 
       viewport =     { 
        northeast =      { 
         lat = "53.3608191"; 
         lng = "-6.1894282"; 
        }; 
        southwest =      { 
         lat = "53.34522"; 
         lng = "-6.2549399"; 
        }; 
       }; 
      }; 
      "place_id" = "ChIJr9h9M_oOZ0gRrycKzPZj46A"; 
      types =    (
       neighborhood, 
       political 
      ); 
     }, 
       { 
      "address_components" =    (
           { 
        "long_name" = "Dublin Northside"; 
        "short_name" = "Dublin Northside"; 
        types =      (
         neighborhood, 
         political 
        ); 
       }, 
           { 
        "long_name" = Ashtown; 
        "short_name" = Ashtown; 
        types =      (
         "sublocality_level_1", 
         sublocality, 
         political 
        ); 
       }, 
           { 
        "long_name" = Dublin; 
        "short_name" = Dublin; 
        types =      (
         locality, 
         political 
        ); 
       }, 
           { 
        "long_name" = "Dublin City"; 
        "short_name" = "Dublin City"; 
        types =      (
         "administrative_area_level_2", 
         political 
        ); 
       }, 
           { 
        "long_name" = Dublin; 
        "short_name" = Dublin; 
        types =      (
         "administrative_area_level_1", 
         political 
        ); 
       }, 
           { 
        "long_name" = Ireland; 
        "short_name" = IE; 
        types =      (
         country, 
         political 
        ); 
       } 
      ); 
      "formatted_address" = "Dublin Northside, Ashtown, Dublin, Ireland"; 
      geometry =    { 
       bounds =     { 
        northeast =      { 
         lat = "53.4088667"; 
         lng = "-6.0358715"; 
        }; 
        southwest =      { 
         lat = "53.34558130000001"; 
         lng = "-6.381769299999999"; 
        }; 
       }; 
       location =     { 
        lat = "53.3815507"; 
        lng = "-6.1922052"; 
       }; 
       "location_type" = APPROXIMATE; 
       viewport =     { 
        northeast =      { 
         lat = "53.4088667"; 
         lng = "-6.0358715"; 
        }; 
        southwest =      { 
         lat = "53.34558130000001"; 
         lng = "-6.381769299999999"; 
        }; 
       }; 
      }; 
      "place_id" = ChIJvUSkJbcPZ0gRyXXPH9MSQwk; 
      types =    (
       neighborhood, 
       political 
      ); 
     }, 
       { 
      "address_components" =    (
           { 
        "long_name" = Dublin; 
        "short_name" = Dublin; 
        types =      (
         locality, 
         political 
        ); 
       }, 
           { 
        "long_name" = "Dublin City"; 
        "short_name" = "Dublin City"; 
        types =      (
         "administrative_area_level_2", 
         political 
        ); 
       }, 
           { 
        "long_name" = Dublin; 
        "short_name" = Dublin; 
        types =      (
         "administrative_area_level_1", 
         political 
        ); 
       }, 
           { 
        "long_name" = Ireland; 
        "short_name" = IE; 
        types =      (
         country, 
         political 
        ); 
       } 
      ); 
      "formatted_address" = "Dublin, Ireland"; 
      geometry =    { 
       bounds =     { 
        northeast =      { 
         lat = "53.42521010000001"; 
         lng = "-6.0439235"; 
        }; 
        southwest =      { 
         lat = "53.22343009999999"; 
         lng = "-6.4474846"; 
        }; 
       }; 
       location =     { 
        lat = "53.3498053"; 
        lng = "-6.2603097"; 
       }; 
       "location_type" = APPROXIMATE; 
       viewport =     { 
        northeast =      { 
         lat = "53.42521010000001"; 
         lng = "-6.0439235"; 
        }; 
        southwest =      { 
         lat = "53.22343009999999"; 
         lng = "-6.4474846"; 
        }; 
       }; 
      }; 
      "place_id" = ChIJL6wn6oAOZ0gRoHExl6nHAAo; 
      types =    (
       locality, 
       political 
      ); 
     }, 
       { 
      "address_components" =    (
           { 
        "long_name" = "Dublin City"; 
        "short_name" = "Dublin City"; 
        types =      (
         "administrative_area_level_2", 
         political 
        ); 
       }, 
           { 
        "long_name" = Dublin; 
        "short_name" = Dublin; 
        types =      (
         "administrative_area_level_1", 
         political 
        ); 
       }, 
           { 
        "long_name" = Ireland; 
        "short_name" = IE; 
        types =      (
         country, 
         political 
        ); 
       } 
      ); 
      "formatted_address" = "Dublin City, Co. Dublin, Ireland"; 
      geometry =    { 
       bounds =     { 
        northeast =      { 
         lat = "53.4111566"; 
         lng = "-6.1131916"; 
        }; 
        southwest =      { 
         lat = "53.2988569"; 
         lng = "-6.3870807"; 
        }; 
       }; 
       location =     { 
        lat = "53.3603142"; 
        lng = "-6.315054200000001"; 
       }; 
       "location_type" = APPROXIMATE; 
       viewport =     { 
        northeast =      { 
         lat = "53.4111566"; 
         lng = "-6.1131916"; 
        }; 
        southwest =      { 
         lat = "53.2988569"; 
         lng = "-6.3870807"; 
        }; 
       }; 
      }; 
      "place_id" = ChIJv2RI7foRZ0gRwAKA8azHAAM; 
      types =    (
       "administrative_area_level_2", 
       political 
      ); 
     }, 
       { 
      "address_components" =    (
           { 
        "long_name" = Dublin; 
        "short_name" = Dublin; 
        types =      (
         "administrative_area_level_1", 
         political 
        ); 
       }, 
           { 
        "long_name" = Ireland; 
        "short_name" = IE; 
        types =      (
         country, 
         political 
        ); 
       } 
      ); 
      "formatted_address" = "Co. Dublin, Ireland"; 
      geometry =    { 
       bounds =     { 
        northeast =      { 
         lat = "53.6347257"; 
         lng = "-5.9962748"; 
        }; 
        southwest =      { 
         lat = "53.1781971"; 
         lng = "-6.5468798"; 
        }; 
       }; 
       location =     { 
        lat = "53.3824769"; 
        lng = "-6.3133674"; 
       }; 
       "location_type" = APPROXIMATE; 
       viewport =     { 
        northeast =      { 
         lat = "53.6347257"; 

         lng = "-5.9962748"; 
        }; 
        southwest =      { 
         lat = "53.1781971"; 
         lng = "-6.5468798"; 
        }; 
       }; 
      }; 
      "place_id" = ChIJv2RI7foRZ0gRwAKA8azHABg; 
      types =    (
       "administrative_area_level_1", 
       political 
      ); 
     }, 
       { 
      "address_components" =    (
           { 
        "long_name" = Ireland; 
        "short_name" = IE; 
        types =      (
         country, 
         political 
        ); 
       } 
      ); 
      "formatted_address" = Ireland; 
      geometry =    { 
       bounds =     { 
        northeast =      { 
         lat = "55.4351345"; 
         lng = "-5.994700099999999"; 
        }; 
        southwest =      { 
         lat = "51.4199312"; 
         lng = "-10.66958"; 
        }; 
       }; 
       location =     { 
        lat = "53.41291"; 
        lng = "-8.24389"; 
       }; 
       "location_type" = APPROXIMATE; 
       viewport =     { 
        northeast =      { 
         lat = "55.4351345"; 
         lng = "-5.99471"; 
        }; 
        southwest =      { 
         lat = "51.4199312"; 
         lng = "-10.6694501"; 
        }; 
       }; 
      }; 
      "place_id" = "ChIJ-ydAXOS6WUgRCPTbzjQSfM8"; 
      types =    (
       country, 
       political 
      ); 
     } 
    ); 
    status = OK; } 
+0

就可以登錄了'geocodeDictionary',看看你會得到什麼 –

+0

你循環:'爲(*的NSDictionary在addressComponentDictionary addressComponents)'這就是爲什麼你會得到最後的價值。如果你不明白爲什麼,在執行'longName = addressComponentDictionary [@「long_name」];''之前和之後,將log記錄在循環中,打印'addressComponents','longName'和'shortName'的值。 @ sken3r.MI:http://maps.googleapis.com/maps/api/geocode/json?latlng=53.348623,-6.243089&sensor=false – Larme

+0

剛剛在那裏添加了JSON日誌。謝謝Larme,我會嘗試一下,看看我如何繼續。你建議刪除循環? –

回答

2

您的循環會在所有address_components,並賦予它們的值將變爲您的longNameshortName變量,因此當循環結束時,只會存儲數組中最後一次檢索到的值。如果你只想要第一套,那麼你可能想要做這樣的事情:

if (addressComponents.count > 0) { 
    NSDictionary *componentDict = addressComponents[0]; 
    longName = componentDict[@"long_name"]; 
    shortName = componentDict[@"short_name"]; 
} 
+0

謝謝,讓它工作。完善。 –

-1
//The address_components array contains many dictionaries, 
     //we loop through each dictionary and check the types array 
     for (NSDictionary *addressComponentDictionary in addressComponents) { 

       longName = addressComponentDictionary[@"long_name"]; 
       shortName = addressComponentDictionary[@"short_name"]; 

     } 

爲Larme說你的問題是在這裏,而不是longNameshortName。 創建2個陣列:

NSMutableArray *longNameArray = [[NSMutableArray alloc] init] 
NSMutableArray *shortNameArray = [[NSMutableArray alloc] init] 
for (NSDictionary *addressComponentDictionary in addressComponents) { 
    [longNameArray addObject:addressComponentDictionary[@"long_name"]]; 
    [shortNameArray addObject:addressComponentDictionary[@"short_name"]]; 
    } 

這將使你在請求中找到的元素的完整列表。

更新 - 僅獲得第一個元素

如果你只需要第一個元素,從陣列獲得的第一項

//This check will ensure that the array is not empty 
if (addressComponents.count > 0) { 
    NSDictionary *addressComponentDictionary = addressComponents[0]; 
    longName = addressComponentDictionary[@"long_name"]; 
    shortName = addressComponentDictionary[@"short_name"]; 
}