ios - 如何从 JSON 输出数据中解码路线点?

标签 ios xcode json google-maps mkmapview

我是ios领域的新人,现在我正在尝试实现基于MKMapView的iPhone应用程序。从 Google Map Web 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 输出数据中分离出所有纬度和经度点。

最佳答案

查看此代码。我正在使用 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"]);

    }

}  

关于ios - 如何从 JSON 输出数据中解码路线点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10100110/

相关文章:

python - 使用 JSON 将两个 API 调用合并在一起

ios - 使用 AutoLayout 仅旋转 View

iphone - 如何在 Xcode 中使用带有嵌套静态库的调试器?

ios - 如何构建具有可折叠部分的表格 View ?

xcode - 检查 Parse.com 中是否存在图像

ios - Swift 用于 JSONEncoder 的默认日期格式是什么?

jquery - 通过 ajax 发送 FormData 对象和附加参数

javascript - $location.Path 不在 iphone 上运行

ios - 在独立 SDK 中注册内存警告通知

ios - 使用 UIVisualEffect,模糊 View 有时不会更新