因此,如果我使用overview_polyline,它可以正常工作。但是,它并不包含所有点,因此我选择循环遍历所有步骤并附加其路径以创建大而精确的路径。出于某种原因,从芝加哥到洛杉矶出现的路径很远,看起来像这样:
这是我的代码:
- (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]];
}];
}
答案 0 :(得分:0)
因此,仅仅通过附加折线点来创建一条新路径似乎不起作用 - 我必须遍历每条折线,提取构成点的坐标,并将其添加到新的大点可变路径。它适用。