根据车辆路线获取从一个位置到另一位置的路线

时间:2019-10-29 08:27:14

标签: ios navigation mapkit direction turn-by-turn

我正在开发一个iOS应用,必须从一个位置到另一个位置获取路线。我正在使用MKDirectionsRequest API获得指导。该api提供了两个位置之间的方向。但是问题在于,此api返回的路线未考虑车辆行驶的方向(路线)。有没有办法在iOS中获得用户当前位置到目的地之间的路线,这也考虑了移动车辆的路线。目前,我正在使用以下功能:

-(void)displayDirections{

    CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:currentLocationLatitude longitude:currentLocationLongitude];
    MKPlacemark *placemarker0 = [[MKPlacemark alloc]initWithCoordinate:currentLocation.coordinate];
    MKMapItem *myMapItem0 = [[MKMapItem alloc] initWithPlacemark:placemarker0];

    CLLocation *location = [[CLLocation alloc] initWithLatitude:destinationLocationLatitude longitude:destinationLocationLongitude]
    MKPlacemark *placemarker1 = [[MKPlacemark alloc]initWithCoordinate:location.coordinate];
    MKMapItem *myMapItem1 = [[MKMapItem alloc] initWithPlacemark:placemarker1];

    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];

    [request setSource:myMapItem0];
    [request setDestination:myMapItem1];

    [request setTransportType:MKDirectionsTransportTypeAny]; 
    [request setRequestsAlternateRoutes:YES]; // Gives you several route options.
    MKDirections *directions = [[MKDirections alloc] initWithRequest:request];

    __weak COMSiteDetailViewController *weakSelf = self;

   [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {

        if (!error) {

            [weakSelf.mkMapView removeOverlays:weakSelf.mkMapView.overlays];
            NSArray *routes = [response routes];
            NSArray *sortedArray;
            sortedArray = [routes sortedArrayUsingComparator:^NSComparisonResult(MKRoute *a, MKRoute *b) {
                CLLocationDistance first = a.distance;
                CLLocationDistance second = b.distance;
                return first < second;
            }];

            int count = 1;
            for (MKRoute *route in sortedArray) {

                if(count < sortedArray.count){
                    route.polyline.title = @"longer";
                }else{
                    route.polyline.title = @"shortest";
                }
                count++;
                [self.mkMapView addOverlay:[route polyline] level:MKOverlayLevelAboveRoads]; 
            }
        }
    }];
}

谢谢!

1 个答案:

答案 0 :(得分:0)

搜索网络后,我发现Google API或Apple MapKit Directions API在提供路线信息时都没有考虑车辆的行驶方向或运动。它仅考虑起点/终点位置的坐标并给出最短的路线。

因此,目前无法使用Apple / Google Apis显示考虑车辆行驶方向的路线。

尽管如此,我在应用程序中还使用了其他付费的Apis,例如https://developer.here.com。该API非常可靠,定价也非常合理。

谢谢!