繁体   English   中英

iOS Google Maps API路由无法正确显示

[英]iOS Google Maps API Route Not Showing Up Correctly

因此,如果我使用overview_polyline,则它可以正常工作。 但是,它并不包含所有要点,因此我选择遍历所有步骤并附加其路径以形成较大的准确路径。 出于某种原因,从芝加哥到洛杉矶的路线似乎偏远,如下所示:

http://imgur.com/avaMeP2

这是我的代码:

- (IBAction)showRoute {
    [OTGNetworkRequest fetchRouteWithStart:self.startLocation withEnd:self.endLocation success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *json = responseObject;
        NSArray *routes = json[@"routes"];
        NSString *encodedOverviewPath = @"";

        [self.routes removeAllObjects];
        self.routes = [[NSMutableArray alloc] init];

        for (int i = 0; i < routes.count; i++) {
            NSArray *legs = routes[i][@"legs"];
            NSMutableArray *steps = [[NSMutableArray alloc] init];
            NSInteger distance = 0;
            NSInteger travelTime = 0;
            for (int j = 0; j < legs.count; j++) {
                distance += [legs[j][@"distance"][@"value"] intValue];
                travelTime += [legs[j][@"duration"][@"value"] intValue];
                [steps addObjectsFromArray:legs[j][@"steps"]];
                for (int l = 0; l < steps.count; l++) {
                    encodedOverviewPath = [encodedOverviewPath stringByAppendingString:steps[l][@"polyline"][@"points"]];

                }
            }

            NSDictionary *route = @{
                                    @"path": [GMSPath pathFromEncodedPath:encodedOverviewPath],
                                    @"distance": [NSNumber numberWithInteger:distance],
                                    @"travelTime" : [NSNumber numberWithInteger:travelTime],
                                    @"steps" : steps};

            [self.routes addObject:route];

        }

        // For now, just take the first route in the list
        NSInteger routeIndex = 0;
        self.routeLine.map = nil;
        self.routeLine = [GMSPolyline polylineWithPath:self.routes[routeIndex][@"path"]];
        self.routeLine.strokeColor = [UIColor blueColor];
        self.routeLine.strokeWidth = 3;
        self.routeLine.map = self.mapView;

        // Converting distance and time to readable values
        NSInteger distance = [self.routes[routeIndex][@"distance"] intValue];
        NSInteger travelTime = [self.routes[routeIndex][@"travelTime"] intValue];

        self.distance = [self convertMetersToMiles:distance];
        self.travelTime = [self convertSecondsToHoursMinutes:travelTime];
        // Update yelp Search View with new distance and time
        self.estimatedDistanceAndTimeLabel.text = [NSString stringWithFormat:@"Distance: %@, Travel Time: %@", self.distance, self.travelTime];

        GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithPath:self.routes[routeIndex][@"path"]];
        [self.mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:50]];
    }];
}

因此,似乎仅仅通过添加折线点来创建新路径是行不通的-我不得不遍历每条折线,提取组成点的坐标,然后将其添加到新的大可变路径中。 然后就可以了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM