0

我需要在使用 Maps API 的 iOS 应用程序中显示多条路线。我可以绘制单条路线,但如何绘制多条路线?

我正在使用谷歌方向 api 来获取单条路线

http://maps.googleapis.com/maps/api/directions/json?origin=28.6353080000,77.2249600000&destination=28.5355161000,77.3910265000&mode=walking&sensor=false

同样在 iPhone 的 ios 5 Native Map 应用程序中显示两个弹出窗口,说 Route1 和 Route 2,当用户触摸选定的路线时会突出显示。所以我们也可以这样做吗?

4

2 回答 2

0

Alternatives(可选),如果设置为 true,则指定 Directions 服务可以在响应中提供多个路线备选方案。请注意,提供备选路线可能会增加服务器的响应时间。

来自Google 路线 API

您需要在查询链接中添加 Alternatives=true

参见:查找两地之间的路线数

于 2012-10-11T04:32:50.567 回答
0

这是代码

写这段代码

#pragma mark - MapView Delegate

//-----------------------------------------------------------------------

    - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {

        //   [self.mapView removeAnnotation:self.annotation];

            NSString *baseUrl = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=true", self.mapView.userLocation.location.coordinate.latitude, self.mapView.userLocation.location.coordinate.longitude, [[self.dictData valueForKey:@"latitude"]doubleValue],[[self.dictData valueForKey:@"longitude"]doubleValue]];

            NSURL *url = [NSURL URLWithString:[baseUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

            NSURLRequest *request = [NSURLRequest requestWithURL:url];

            [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

                NSError *error = nil;
                NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

                NSArray *routes = [result objectForKey:@"routes"];

                NSDictionary *firstRoute = [routes objectAtIndex:0];

                NSDictionary *leg = [[firstRoute objectForKey:@"legs"] objectAtIndex:0];

                NSDictionary *end_location = [leg objectForKey:@"end_location"];

                double latitude = [[end_location objectForKey:@"lat"] doubleValue];
                double longitude = [[end_location objectForKey:@"lng"] doubleValue];

                CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);

                MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
                point.coordinate = coordinate;
                point.title = [leg objectForKey:@"end_address"];
                point.subtitle = @"Event Destinations !!!";

                [self.mapView addAnnotation:point];

                NSArray *steps = [leg objectForKey:@"steps"];

                int stepIndex = 0;

                CLLocationCoordinate2D stepCoordinates[1 + [steps count] + 1];

                stepCoordinates[stepIndex] = userLocation.coordinate;

                for (NSDictionary *step in steps) {

                    NSDictionary *start_location = [step objectForKey:@"start_location"];
                    stepCoordinates[++stepIndex] = [self coordinateWithLocation:start_location];

                    if ([steps count] == stepIndex){
                        NSDictionary *end_location = [step objectForKey:@"end_location"];
                        stepCoordinates[++stepIndex] = [self coordinateWithLocation:end_location];
                    }
                }

                MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:stepCoordinates count:1 + stepIndex];
                [self.mapView addOverlay:polyLine];

            }];
    }

#pragma mark - Custom Methods

//-----------------------------------------------------------------------

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
    MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
    polylineView.strokeColor = [UIColor colorWithRed:204/255. green:45/255. blue:70/255. alpha:1.0];
    polylineView.lineWidth = 10.0;

    return polylineView;
}
于 2015-04-30T12:21:18.763 回答