Load Directions (iOS) Follow
This article describes how to load directions using the iOS SDK.
Load Directions
MRMapViewController handles loading and presenting directions in response to user interactions.
When you create an instance of MRMapViewController, you can set the pendingDestination property to initiate directions to that MRPlacemark as soon as the map view is visible.
If you want to present directions modally, the SDK provides a convenient way to do so with an instance method on UIViewController(http://files.meridianapps.com/meridian-ios-sdk/docs-latest/Categories/UIViewController+Directions.html).
// Somewhere in your custom view controller // Assume myPlacemark is a valid MRPlacemark instance [self presentDirectionsToPlacemark:myPlacemark];
If you’re using MRMapView directly in your own view controller, you’ll need to handle loading directions manually. You can do this using MRDirectionsRequest. Specify the destination and starting point for the route using instances of MRMapItem
.
Like MapKit’s [MKMapItem][MKMapItem],
MRMapItem
contains information about a point on a map.
After configuring the request with the details of the desired route, you can use it to create an instance of MRDirections and then call calculateDirectionsWithCompletionHandler: to asynchronously calculate directions for the route.
Finally, provide a completion block to be executed when the request is complete, to handle the response and any errors.
// Somewhere in your view controller // Assume myPlacemark is a valid MRPlacemark instance MRDirectionsRequest *request = [MRDirectionsRequest new]; request.source = [MRDirectionsSource sourceWithCurrentLocation]; request.destination = [MRDirectionsDestination destinationWithMapKey:placemark.key.parent withPoint:placemark.point]; request.transportType = MRDirectionsTransportTypeAny; MRDirections *directions = [[MRDirections alloc] initWithRequest:request presentingView:self.view]; [directions calculateDirectionsWithCompletionHandler:^(MRDirectionsResponse *response, NSError *error) { if (!error) { if (response.routes.count 0) { // Normally only one route will be returned MRRoute *route = response.routes.firstObject; // If we have an MRMapView we can tell it to display this route [self.mapView setRoute:route animated:YES]; } else { NSLog(@"Error loading directions: %@", error.localizedDescription); } } }];
In the example, MRDirectionsSource sourceWithCurrentLocation indicates the route should start at the user’s current location. When creating the ‘MRDirections’ instance, the controller’s view is passed as the presentingViewController: parameter.
A new route is calculated when the user’s blue dot location is approximately 15 meters from the route.
When a valid current location is not available, this causes MRDirections to prompt the user to select a starting location with a modal search interface.
Comments
0 comments
Please sign in to leave a comment.