Maybe somebody can help. I am trying to get the location name for the google maps location and latitude in the URL below. As you can see from the JSON that it is first opening the results then the address_components. I am trying to get the first long_name and short_name in the JSON but instead the code I have below is only giving me the last long_name and short_name in the JSON.
Maybe someone has some type of idea, I have tried everything at this stage and nothing seems to solve the issue.
Thanks,
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; }
Your for loop is going over all of the address_components
and assigning their values to your longName
and shortName
variables, thus when the loop finishes only the last retrieved values in the array will be stored. If you only want the first set then you probably want to do something like this:
if ( addressComponents.count > 0 ) {
NSDictionary *componentDict = addressComponents[0];
longName = componentDict[@"long_name"];
shortName = componentDict[@"short_name"];
}