First We have add JSON framework on your project code (if u need JSON Code download here : http://www.touch-code-magazine.com/tutorial-fetch-and-parse-json/) tutorial: json_demo1
NSString *address = @"Chennai";
NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
NSLog(@"result==%@",result);
NSDictionary *tempDict=nil;
if (result&&[result length]!=0){
NSArray* latestResult = [(NSDictionary*)[result JSONValue] objectForKey:@"results"];
NSLog(@"result1==%@", latestResult);
for (NSDictionary *dict in latestResult) {
tempDict = [[dict objectForKey:@"geometry"]objectForKey:@"location"];
break;
}
}
NSLog(@"latitude == %f & longitude == %f",[[tempDict objectForKey:@"lat"]doubleValue], [[tempDict objectForKey:@"lng"]doubleValue] );
Output:
Output:
json_demo1[2425:ef03] resukt=={
"results" : [
{
"address_components" : [
{
"long_name" : "Chennai",
"short_name" : "Chennai",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Chennai",
"short_name" : "Chennai",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Chennai, Tamil Nadu, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 13.23408510,
"lng" : 80.33229129999999
},
"southwest" : {
"lat" : 12.94592670,
"lng" : 80.14878270
}
},
"location" : {
"lat" : 13.0604220,
"lng" : 80.2495830
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 13.14736150,
"lng" : 80.37764240
},
"southwest" : {
"lat" : 12.97345190,
"lng" : 80.12152360
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
2012-07-10 12:09:06.539 json_demo1[2425:ef03] resukt1==(
{
"address_components" = (
{
"long_name" = Chennai;
"short_name" = Chennai;
types = (
locality,
political
);
},
{
"long_name" = Chennai;
"short_name" = Chennai;
types = (
"administrative_area_level_2",
political
);
},
{
"long_name" = "Tamil Nadu";
"short_name" = TN;
types = (
"administrative_area_level_1",
political
);
},
{
"long_name" = India;
"short_name" = IN;
types = (
country,
political
);
}
);
"formatted_address" = "Chennai, Tamil Nadu, India";
geometry = {
bounds = {
northeast = {
lat = "13.2340851";
lng = "80.33229129999999";
};
southwest = {
lat = "12.9459267";
lng = "80.1487827";
};
};
location = {
lat = "13.060422";
lng = "80.249583";
};
"location_type" = APPROXIMATE;
viewport = {
northeast = {
lat = "13.1473615";
lng = "80.3776424";
};
southwest = {
lat = "12.9734519";
lng = "80.1215236";
};
};
};
types = (
locality,
political
);
}
)
2012-07-10 12:09:06.539 json_demo1[2425:ef03] latitude == 13.060422 & longitude == 80.249583
No comments:
Post a Comment