iOS MapKit 通过道路连接地图上的多个点

Posted

技术标签:

【中文标题】iOS MapKit 通过道路连接地图上的多个点【英文标题】:iOS MapKit connect multiple points on map over roads 【发布时间】:2014-06-25 03:34:15 【问题描述】:

我正在使用此代码将引脚(地图上的点)与折线连接:

CLLocationCoordinate2D coordinates[allLocations.count];
    int i = 0;
    for (locHolder *location in allLocations) 
        coordinates[i] = CLLocationCoordinate2DMake([location.lat floatValue], [location floatValue]);
        i++;
    
    MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coordinates count:[allLocations count]];
    self->polyline = polyline;
    [self.mapView addOverlay:self->polyline level:MKOverlayLevelAboveRoads];

但是这段代码通过无线方式连接它们(忽略道路),是否可以将多个 CLLocationCoordinate2D 位置与跟随道路的折线连接?

** 还有一个简短的问题, [allLocations count] 和 allLocations.count 之间的性能是否存在差异。

谢谢。

【问题讨论】:

使用 MKDirectionsRequest 获取位置之间的行车路线。有关示例,请参阅***.com/questions/18805613/…。此外,[allLocations count]allLocations.count 完全相同(参见***.com/questions/1249392/…)。 我已经查看了 MKDirectionsRequest,但它允许连接点 A 和 B,但我需要的是通过 B、C、D、E 连接点 A 和 X...达到了吗? A 到 B 调用它,然后 B 到 C 调用它,然后...... 【参考方案1】:

这几天我遇到了同样的问题,到处寻找答案,我发现thisRob的答案对这个案例非常有用。

首先,假设您的 MapViewController 中有一个包含起点和终点的对象数组,每个对象的类型均为 CLLocationCoordinate2D

在你的 MapViewController.h

@property (nonatomic, strong) NSArray *locations;

然后,在实现中,使用如下所示的一些数据填充该数组:

_locations = [NSArray arrayWithObjects:
    [RouteObject routeWithOrigin:CLLocationCoordinate2DMake(-23.595571, -46.684408) destination:CLLocationCoordinate2DMake(-23.597886, -46.673950)],
    [RouteObject routeWithOrigin:CLLocationCoordinate2DMake(-23.597886, -46.673950) destination:CLLocationCoordinate2DMake(-23.597591, -46.666805)],
    [RouteObject routeWithOrigin:CLLocationCoordinate2DMake(-23.597591, -46.666805) destination:CLLocationCoordinate2DMake(-23.604061, -46.662728)], nil];

现在您已经有了起点和终点的数组,您可以开始在它们之间绘制一条路线(从 A 到 B、从 B 到 C 以及从 C 到 D)。

添加一个按钮,然后连接一个IBAction,这样您就可以使用这种方法来发挥作用了。

- (IBAction)btnDirectionsPressed:(id)sender 
    [self enableUI:NO];    // This method enables or disables all the UI elements that interact with the user

    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);     // Create the semaphore    

    __block NSMutableArray *routes = [[NSMutableArray alloc] init];         // Arrays to store MKRoute objects and MKAnnotationPoint objects, so then we can draw them on the map
    __block NSMutableArray *annotations = [[NSMutableArray alloc] init];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^
        for (RouteObject *routeObject in _locations) 
            MKDirectionsRequest *directionsRequest = [MKDirectionsRequest new];
            [directionsRequest setTransportType:MKDirectionsTransportTypeAutomobile];
            [directionsRequest setSource:[routeObject originMapItem]];
            [directionsRequest setDestination:[routeObject destinationMapItem]];
            [directionsRequest setRequestsAlternateRoutes:NO];

            MKDirections *direction = [[MKDirections alloc] initWithRequest:directionsRequest];

            // For each object in the locations array, we request that route from its origin and its destination
            [direction calculateDirectionsWithCompletionHandler: ^(MKDirectionsResponse *response, NSError *error)    
                if (error) 
                    NSLog(@"There was an error getting your directions");
                    return;
                

                MKRoute *route = [response.routes firstObject];   
                [routes addObject:route];
                [annotations addObject:[routeObject destinationAnnotation]];

                dispatch_semaphore_signal(semaphore);    // Send the signal that one semaphore is ready to consume
            ];
        
    );

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^
        for (int i = 0; i < _locations.count; i++) 
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);    // Wait until one semaphore is ready to consume

            dispatch_async(dispatch_get_main_queue(), ^    // For each element, dispatch to the main queue to draw route and annotation corresponding to that location
                MKRoute *route = routes[i];
                [self.mapView addOverlay:route.polyline];
                [self.mapView addAnnotation:annotations[i]];
            );
        

        dispatch_async(dispatch_get_main_queue(), ^   // Finally, dispatch to the main queue enabling elements and resizing map to show all the annotations
            [self enableUI:YES];
            [self fitRegionToRoutes];
        );
    );

希望对您有所帮助!

乔尔。

【讨论】:

嗨,经过多年的差距,我正在开发 ios。尝试运行此代码并给出错误为“使用未声明的标识 RouteObject”。我已经包含了 Mapkit 和 CoreLocation 框架并导入到文件中。请建议。我想在起点和终点以外的多个点之间创建一个查找距离。 @Pawriwes RouteObject 是我为此目的制作的一些 POJO。它有两个属性,都是 CLLocationCoordinate2D 类型,还有一些有用的方法。

以上是关于iOS MapKit 通过道路连接地图上的多个点的主要内容,如果未能解决你的问题,请参考以下文章

IOS/MapKit:通过单击 MKPlacemark 启动本机地图应用程序

iOS开发学习之MapKit - 获得在MapView(地图)中显示多个标记的区域(MKCoordinateRegion)

Swift mapkit未指向位置

使用 MapKit 在 IOS 地图上放置图钉? [关闭]

在 iOS MapKit 中,如何在地图旋转时为注释设置动画?

iOS核心笔记—MapKit框架-基础