2

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.

Musthafa
  • 862
  • 3
  • 12
  • 27

2 Answers2

2

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"]);

    }

}  
Hiren
  • 12,720
  • 7
  • 52
  • 72
  • thanks too much .I am using JSON Framework. So i edited NSMutableDictionary *data1 = [responseData objectFromJSONData]; to to NSString *responseString=[[NSString alloc]initWithData:self.responseData encoding:NSUTF8StringEncoding]; – Musthafa Apr 11 '12 at 07:07
0

I am using SBJson to parse json data. In general, call [responseString JSONValue] to get parsed data. You can google it for detail reference.

yibuyiqu
  • 728
  • 1
  • 7
  • 20