【问题标题】:Parsing Json in Google Maps iOS在 Google 地图 iOS 中解析 Json
【发布时间】:2014-11-10 20:39:34
【问题描述】:

我收到了 Json 响应。这里是:

Directions {
routes =     (
            {
        bounds =             {
            northeast =                 {
                lat = "39.893397";
                lng = "42.2477961";
            };
            southwest =                 {
                lat = "21.4204199";
                lng = "39.2398944";
            };
        };
        copyrights = "Map data \U00a92014 Google, Mapa GISrael, ORION-ME";
        legs =             (
                            {
                distance =                     {
                    text = "2,940 km";
                    value = 2940341;
                };
                duration =                     {
                    text = "1 day 15 hours";
                    value = 138793;
                };
                "end_address" = "Unnamed Road, Erzincan Province, Turkey";
                "end_location" =                     {
                    lat = "39.893397";
                    lng = "39.906191";
                };
                "start_address" = "4289, Al Hajlah, Mecca 24231\U00a06970, Saudi Arabia";
                "start_location" =                     {
                    lat = "21.4204199";
                    lng = "39.8258119";
                };
                steps =                     (
                                            {
                        distance =                             {
                            text = "0.6 km";
                            value = 630;
                        };
                        duration =                             {
                            text = "1 min";
                            value = 55;
                        };
                        "end_location" =                             {
                            lat = "21.4235672";
                            lng = "39.8211613";
                        };
                        "html_instructions" = "Head <b>west</b> on <b>Alsouq Alsagheer Tunnel</b>";
                        polyline =                             {
                            points = "stvaCinarFEh@?h@?f@Cd@If@Gb@Yx@Yx@e@bAWb@Y`@CD[b@c@l@ILADIRIVGPKRGJKPILKPWRi@X_Ad@MFSJ]N[N_@P]ROJ";
                        };
                        "start_location" =                             {
                            lat = "21.4204199";
                            lng = "39.8258119";
                        };
                        "travel_mode" = DRIVING;

然后继续……

这是我项目中的一些代码:

方向.h

-(void)retrieveDirectionsFromOrigin:(CLLocationCoordinate2D)origin toDestination:(CLLocationCoordinate2D)destination
{
NSString *directionsURL=[NSString stringWithFormat:@"http://maps.google.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=false",origin.latitude,origin.longitude,destination.longitude,destination.longitude];
_directionsURL=[NSURL URLWithString:directionsURL];
[self retrieveDirections:nil withDelegate:self];
}
-(void)retrieveDirections:(SEL)selector withDelegate:(id)delegate{
dispatch_async(dispatch_get_main_queue(), ^{
    NSData *data = [NSData dataWithContentsOfURL:_directionsURL];
    [self fetchedData:data withDelegate:delegate];
});
}

-(void)fetchedData:(NSData *)data withDelegate:(id)delegate{
NSError* error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions    error:&error];
NSLog(@"Directions %@",json);
if(!error){
    self.steps = json[@"routes"][0][@"legs"][0][@"steps"];
}

//MapViewController.m

marker.position = CLLocationCoordinate2DMake(21.422492, 39.826169);
marker2.position = CLLocationCoordinate2DMake(21.413333, 39.893333);
marker.map = mapView;
marker2.map = mapView;

directions=[[Directions alloc]init];
[directions retrieveDirectionsFromOrigin:marker.position toDestination:marker2.position];   
[mapViewView setNeedsDisplay];
[mapView setNeedsDisplay];
[self.view bringSubviewToFront:backButton];

}

我是初学者。请让我知道接下来要做什么以通过折线显示路线。谢谢

【问题讨论】:

  • 不要从 NSJSONSerialization(或任何其他具有 error 参数的 API)测试 error。测试结果,如果为 nil,则转储 error 以显示失败原因。
  • 否则,接下来要做的就是想一个问题。

标签: ios json google-maps parsing polyline


【解决方案1】:

这里有一个关于如何创建折线的提示,它应该可以帮助你开始你的方式

从步骤中收集多字符串

   NSArray *steps = [valDictionary valueForKey:@"steps"];
   for (NSDictionary *stepdic in steps) {
      NSString *polyStr = [[stepdic valueForKey:@"polyline"] valueForKey:@"points"];
       if (polyStr != nil) [polyStrings addObject:polyStr];
    }

从多段字符串中创建一条路径以获得平滑线并创建最终多段线

   GMSMutablePath *path = [GMSMutablePath path];
   for (NSString *polyStr in polyStrings) {
          GMSPath *p = [GMSPath pathFromEncodedPath:polyStr];
          for (NSUInteger i=0; i < p.count; i++) {
                 [path addCoordinate:[p coordinateAtIndex:i]];
          }
   }

  GMSPolyline *polyLint = [GMSPolyline polylineWithPath:path];

祝你好运

【讨论】:

  • 您写道:“if (polyStr != nil) [polyStrings addObject:polyStr];”这段代码中的 polyStrings 是什么?请解释一下。
  • polyStrings 只是一个 NSMutableArray 的 polyString,取自 steps.polyline.points
  • 接下来是什么?我怎样才能画出这个 polyLint ? @StevenVeltema Pelase 协助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多