Load Directions (Android)

This article describes how to load directions using the Android SDK for both a MapFragment and a MapView.

Load Directions with MapFragment

MapFragment handles loading and presenting directions on a map in response to user interaction. There are two ways to load directions using MapFragment.

When you create the MapFragment, include the pendingDestination parameter to present directions to a placemark when the map is opened.

    MapFragment mapFragment = new MapFragment.Builder()
                    .setAppKey(appKey)
                    .setMapKey(mapKey)
                    .setSource(directionsSource)
                    .setDestination(directionsDestination)
                    .build();

Alternately, if you have an existing MapFragment, call startDirections to load directions.

    mapFragment.startDirections(DirectionsDestination.forPlacemarkKey(placemark.key))

Load Directions with MapView

If you're using MapView directly in your activity, you'll need to load the directions manually with the Directions class.

To do so, first ensure that your activity implements the Directions.DirectionsRequestListener interface so that it can handle the results of the directions request. Use a Directions.Builder instance to configure the details of the desired route. To do this, specify the start and end points for the route using instances of DirectionsSource and DirectionsDestination, which contain information about a point or placemark on a map.

Next, call build() to create the Directions object from the builder.

Finally, call calculate() to start the directions process.

The Directions object will call onDirectionsComplete() after successful completion or onDirectionsError() if something went wrong.

MapView Code Example

In the following code example, MeridianLocation is the source of the directions.

A new route is calculated when the user's blue dot location is approximately 10 meters from the route. To customize the reroute distance, in build.gradle set MERIDIAN_DEFAULT_ROUTE_SNAP to half the desired rerouting distance.

For example, if you want the rerouting to start at 20 meters, set the MERIDIAN_DEFAULT_ROUTE_SNAP value to 10.

You can also use Meridian.getShared().setRouteSnapDistance() to set this value.

    // In your Activity

    private static final EditorKey appKey = new EditorKey("YOUR_APP_KEY");

    private void getDirections(MeridianLocation source, Placemark destination) {
        Directions.Builder directionsBuilder = new Directions.Builder()
                .setAppKey(appKey)
                .setSource(DirectionsSource.forMapPoint(source.getMap(), source.getPoint()))
                .setDestination(DirectionsDestination.forPlacemarkKey(destination.getKey()))
                .setTransportType(TransportType.WALKING)
                .setListener(this);

        Directions directions = directionsBuilder.build();
        directions.calculate();
    }

    @Override
    public void onDirectionsComplete(DirectionsResponse response) {
        if (response.getRoutes().size() > 0) {

            // Normally only one route will be returned
            Route route = response.getRoutes().get(0);

            // If we have a MapView we can tell it to display this route
            myMapView.setRoute(route);
        }
    }

If a current location isn't available, you may want to prompt the user to choose a starting location with SearchActivity.

In the example, SearchActivity and startActivityForResult() will prompt the user to choose a starting placemark.

    // In your Activity

    private Directions.Builder directionsBuilder;
    private static final EditorKey appKey = new EditorKey("5085468668573440");
    public static final int PLACEMARK_PICKER_CODE = 42;

    private void getDirections(Placemark placemark) {
        directionsBuilder = new Directions.Builder()
            .setContext(this)
            .setAppKey(appKey)
            .setDestination(DirectionsDestination.forPlacemark(placemark))
            .setTransportType(TransportType.WALKING)
            .setListener(this);

        // start a search Activity to get the starting point for the route
        startActivityForResult(SearchActivity.createIntent(this, appKey), PLACEMARK_PICKER_CODE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        // Now that the user picked a placemark to start from, we can calculate the route
        if (requestCode == PLACEMARK_PICKER_CODE) {
            if (resultCode == Activity.RESULT_OK) {

                LocalSearchResult result = (LocalSearchResult) data.getSerializableExtra(SearchActivity.SEARCH_RESULT_KEY);
                Placemark source = result.getPlacemark();
                directionsBuilder.setSource(DirectionsSource.forPlacemark(source));
                directions = directionsBuilder.build();
                directions.calculate();
            }
        }
    }
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.