Search code examples
androiddirections

How do I calculate path between two map points in Android?


I am working on iOS/Android map application.

To find a path between two locations in iOS, I am using MKDirectionsRequest.

How do I do it in Android?

I found Directions API, which is a Web service. So that I will have to send HTTP requests.

Isn't there any Java interface to calculate directions in Android?


Solution

  • To set this as an answer.

    You can use Android-GoogleDirectionLibrary

    enter image description here

    As such:

    GoogleDirection.withServerKey("YOUR_SERVER_API_KEY")
            .from(new LatLng(37.7681994, -122.444538))
                .to(new LatLng(37.7749003,-122.4034934))
                .avoid(AvoidType.FERRIES)
                .avoid(AvoidType.HIGHWAYS)
                .execute(new DirectionCallback() {
        @Override
        public void onDirectionSuccess(Direction direction, String rawBody) {
            if(direction.isOK()) {
                // Do something
            } else {
                // Do something
            }
        }
    
        @Override
        public void onDirectionFailure(Throwable t) {
            // Do something
        }
    });