ateebahmed
ateebahmed

Reputation: 182

How to use Java Client Library Google Maps Services for DirectionsApi

I have tried for several days and could not find any example which uses Java Client Library for Google Maps Services for DirectionsApi, I have seen many tutorials where they work on requet response, but I want to use this library because it is created by Google and is community supported. I saw the Geocoding example on their github page and saw the reference site for library but could not understand how can I implement DirectionsApi. I am using it in Android and geocoding example works fine.

Upvotes: 1

Views: 7348

Answers (1)

Maher Nabil
Maher Nabil

Reputation: 1417

here is a simple snippet

GeoApiContext context = new GeoApiContext().setApiKey("YOUR_API_KEY");

DirectionsApiRequest apiRequest = DirectionsApi.newRequest(context);
apiRequest.origin(new com.google.maps.model.LatLng(latitude, longitude));
apiRequest.destination(new com.google.maps.model.LatLng(latitude, longitude));
apiRequest.mode(TravelMode.DRIVING); //set travelling mode

apiRequest.setCallback(new com.google.maps.PendingResult.Callback<DirectionsResult>() {
    @Override
    public void onResult(DirectionsResult result) {
        DirectionsRoute[] routes = result.routes;

    }

    @Override
    public void onFailure(Throwable e) {

    }
});

To understand the other options, refer to the documentation: https://developers.google.com/maps/documentation/directions/intro

Upvotes: 3

Related Questions