2

我是该ios领域的新手,现在我正在尝试实现MKMapView基于 iPhone 的应用程序。从 Google Map Web 网络服务中,我以JSON输出格式获取数据。

- (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);


}  

我得到了JSON格式中的输出。现在我想在这两个位置之间绘制路线。播种如何从JSON输出数据中分离所有纬度和经度点。

4

2 回答 2

2

查看此代码。我正在使用 JSONkit 进行解析

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

    }

}  
于 2012-04-11T06:12:08.847 回答
0

我正在使用 SBJson 来解析 json 数据。通常,调用[responseString JSONValue]以获取解析的数据。您可以谷歌它以获取详细参考。

于 2012-04-11T06:10:58.847 回答