iOS MapKit通过道路连接地图上的多个点 [英] iOS MapKit connect multiple points on map over roads

查看:132
本文介绍了iOS MapKit通过道路连接地图上的多个点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using this code to connect pins(points on map) with polyline:

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位置,道路后面有折线吗?

But this code connects them over-the-air(ignoring roads), is it possible to connect multiple CLLocationCoordinate2D locations with polyline that follows roads?

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

** And a shor sub question, Is there any difference in performance between [allLocations count] and allLocations.count.

谢谢。

推荐答案

在这些日子里我遇到了同样的问题,并且在这里和那里寻找答案,我从这个答案=https://stackoverflow.com/users/1271826/rob> Rob 对于这种情况非常有用。

In these days I've came across with the same problem, and looking for answers here and there, I found this answer from Rob very useful for this case.

首先,请你有你的MapViewController,一个包含起源和目标的对象数组,每个对象的类型为 CLLocationCoordinate2D

First, supose you have in your MapViewController, an array of objects containing an origin an a destination, each one being of type CLLocationCoordinate2D

在MapViewController中.h

In your MapViewController.h

@property (nonatomic, strong) NSArray *locations;

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

Then, in the implementation, populate that array with some data that goes like this:

_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)。

Now that you have your array of origin and destinations, you can start drawing a route between them (from A to B, from B to C and from C to D).

添加一个按钮,然后连接 IBAction 所以在那种方法中你会做魔术。

Add a button and then connect an IBAction so in that method you are going to do de magic.

- (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];
        });
    });
}

希望这个帮助!

Joel。

这篇关于iOS MapKit通过道路连接地图上的多个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆