问题
I am new in the ios
field, Now i am trying to implement MKMapView
Based iPhone Application. From the Google Map Web web service i Fetch the Data in JSON
Output Format.
- (void)update {
self.responseData=[NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://maps.googleapis.com/maps/api/directions/json?origin=Ernakulam&destination=Kanyakumari&mode=driving&sensor=false"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[self.responseData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.responseData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
[connection release];
self.responseData =nil;
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"connection established");
[connection release];
NSString *responseString=[[NSString alloc]initWithData:self.responseData encoding:NSUTF8StringEncoding];
self.responseData=nil;
NSLog(@" OUTPUT JSON DATA^^^^^^^^^^^^^^^^%@",responseString);
}
I got the out put has in the JSON
Format. Now i want to draw route between this two location. sow how can i separate all the latitude and Longitude points from the JSON
out put Data.
回答1:
Check out this code. I am using JSONkit for parsing
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"connection established");
[connection release];
NSString *responseString=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
NSMutableDictionary *data1 = [responseData objectFromJSONData];
NSMutableArray *ad = [data1 objectForKey:@"routes"];
NSMutableArray *data2 = [[ad objectAtIndex:0] objectForKey:@"legs"];
NSMutableArray *data3 = [[data2 objectAtIndex:0] objectForKey:@"steps"];
// NSLog(@"Data3 %@", data3);
for(int i = 0; i<data3.count;i++){
NSLog(@"Start %@", [[data3 objectAtIndex:i] objectForKey:@"start_location"]);
NSLog(@"End %@", [[data3 objectAtIndex:i] objectForKey:@"end_location"]);
}
}
回答2:
I am using SBJson to parse json data.
In general, call [responseString JSONValue]
to get parsed data.
You can google it for detail reference.
来源:https://stackoverflow.com/questions/10100110/how-to-decode-route-points-from-the-json-output-data