27

我知道谷歌地图被认为是最好的地图,但我不想下载一堆额外的库等等。我宁愿做一些快速简单的事情来获得从 A 点到 B 点的快速路线并完成它。有没有办法使用内置函数/库来做到这一点?有人可以指出我正确的方向吗?

编辑

在我的情况下,我不想通过转弯方向或任何东西转弯,我只是想从头到尾画一条线。也许提供有关采取哪些路线的选项。有没有办法做到这一点?

4

4 回答 4

67

在 iOS 7 中,您可以使用MKDirectionsRequest.

下面是一些示例代码,用于显示从当前位置到另一个地图项的路线:

MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
[request setSource:[MKMapItem mapItemForCurrentLocation]];
[request setDestination:myMapItem];
[request setTransportType:MKDirectionsTransportTypeAny]; // This can be limited to automobile and walking directions.
[request setRequestsAlternateRoutes:YES]; // Gives you several route options.
MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
    if (!error) {
        for (MKRoute *route in [response routes]) {
            [myMapView addOverlay:[route polyline] level:MKOverlayLevelAboveRoads]; // Draws the route above roads, but below labels.
            // You can also get turn-by-turn steps, distance, advisory notices, ETA, etc by accessing various route properties.
        }
    }
}];

如果您是 iOS 7 的新手,则需要实现该mapView:rendererForOverlay:方法才能显示任何叠加层。就像是:

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
        [renderer setStrokeColor:[UIColor blueColor]];
        [renderer setLineWidth:5.0];
        return renderer;
    }
    return nil;
}
于 2013-11-04T17:50:15.873 回答
2

斯威夫特版本

        let request = MKDirectionsRequest();
        request.source = MKMapItem.mapItemForCurrentLocation();
        let locationPlacemark = MKPlacemark(coordinate: CLLocationCoordinate2DMake(13.724362, 100.515342), addressDictionary: nil);
        request.destination = MKMapItem(placemark: locationPlacemark);
        request.transportType = MKDirectionsTransportType.Any;
        request.requestsAlternateRoutes = true;
        let directions = MKDirections(request: request);

        directions.calculateDirectionsWithCompletionHandler ({
            (response: MKDirectionsResponse?, error: NSError?) in
            print(response?.description)
            print(error?.description)
            guard let response = response else {
                //handle the error here
                return;
            }
            self.myRoute = response.routes[0]
            self.mkMapView.addOverlay(self.myRoute!.polyline)
        });

及其代表

        func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
        let myLineRenderer = MKPolylineRenderer(polyline: (self.myRoute?.polyline)!)
        myLineRenderer.strokeColor = UIColor.redColor()
        myLineRenderer.lineWidth = 3
        return myLineRenderer
        }
于 2016-03-22T09:34:19.927 回答
1

另一种可能性是将地址发送到 Apple Maps 应用程序。我刚刚看到这是在专业环境中完成的,这就是选择的方法。

于 2014-05-18T03:48:45.367 回答
-4

如果您想在点击 Pin 时显示警报对话框,请执行以下操作:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view     calloutAccessoryControlTapped:(UIControl *)control {
[mapView deselectAnnotation:view.annotation animated:YES];

    if ([view.annotation isKindOfClass:[PinOfProject class]])
    {
        CLLocationCoordinate2D coordinate = [view.annotation coordinate];
        MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
        MKMapItem *mapitem = [[MKMapItem alloc] initWithPlacemark:placemark];
        self.mapItem = mapitem;

        CGPoint pin = [mapView convertCoordinate:view.annotation.coordinate toPointToView:self.view];
        CGRect rec = CGRectMake(pin.x-13, pin.y-14,view.frame.size.width,view.frame.size.height);


        [self showAlertInformationForTrash:rec];

    }
}

-(void)showAlertInformationForTrash:(CGRect)rec{

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Show Route?" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Route", @"Cancel", nil];
    actionSheet.tag = 1;
    [actionSheet showFromRect:rec inView:self.view animated:YES];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        [self showRouteToAnnotation];
    }
}

-(void)showRouteToAnnotation{
    MKMapItem *myMapItem = self.mapItem;
    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
    [request setSource:[MKMapItem mapItemForCurrentLocation]];
    [request setDestination:myMapItem];
    [request setTransportType:MKDirectionsTransportTypeAutomobile]; // This can be limited to automobile and walking directions.
    [request setRequestsAlternateRoutes:NO]; // Gives you several route options.
    MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
        if (!error) {
            for (MKRoute *route in [response routes]) {
                [self.mapView addOverlay:[route polyline] level:MKOverlayLevelAboveRoads]; // Draws the route above roads, but below labels.
                for (int i = 0; i < route.steps.count; i++) {
                    MKRouteStep *step = [route.steps objectAtIndex:i];
                    NSString *newStep = step.instructions;
                    NSLog(@"%@", newStep);
                }                                                                       
            }
        }
    }];
}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
        [renderer setStrokeColor:[UIColor blueColor]];
        [renderer setLineWidth:5.0];
        return renderer;
    }
    return nil;
}
  • 哦,但请注意,我在我的 .h @property (strong, nonatomic)MKMapItem *mapItem; 中创建了一个属性。
于 2014-09-26T12:07:16.983 回答