【问题标题】:Multiple route between two location in iPhoneiPhone中两个位置之间的多条路线
【发布时间】:2012-10-01 16:23:53
【问题描述】:

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

我正在使用 google 方向 api 获取单条路线

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

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

【问题讨论】:

  • 您好,欢迎来到 SO。展示一个你已经做过的例子是非常典型的。这通常很重要,因为它向人们表明你已经完成了你的功课,并准确地澄清了你的误解在哪里。
  • 我同意一个关于您如何完成第一条路线的示例以及有关您正在使用的库的更多信息将对您有所帮助。我冒昧地猜测您正在使用地图 api。
  • 还有一个提示是,在 ios6 中,谷歌地图在默认情况下是不启用的,你知道的。它已被苹果地图取代。我认为它仍然可以通过应用程序存储下载。
  • @PabloKarlsson 但我与谷歌或苹果等特定地图无关。我正在使用纬度、经度绘制一条线。可能我错了,但我相信它在任何地图上都一样。因为我担心的是拉长
  • 好的,我只是想提高您的认识。

标签: ios objective-c google-maps


【解决方案1】:

替代品(可选),如果设置为 true,指定方向 服务可能提供不止一个 响应中的路线替代方案。 请注意,提供路线选择 可能会增加响应时间 服务器。

来自The Google Directions API

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

见:Find the number of routes between two places

【讨论】:

    【解决方案2】:

    这是代码

    写这段代码

    #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;
    }
    

    【讨论】:

    • 您可能希望改进答案中的代码格式。要阻止引用代码,请将每行缩进 4 个空格。使用反引号“`”仅适用于单词或少量代码。看到了吗?编辑时的按钮以获取更多代码格式提示!
    猜你喜欢
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-19
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多