1

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.

ateebahmed
  • 183
  • 5
  • 19
  • 2
    The library should not be used directly in Android code for security issues surrounding API Key [https://maps-apis.googleblog.com/2016/09/making-most-of-google-maps-web-service.html]. Please introduce a proxy server. And, use Maher Nabeel's excellent example for DirectionsAPI. Hope this helps. – BhalchandraSW Apr 24 '17 at 10:36
  • Readme on github says that this library is an excellent choice for using a proxy server, so how do i go about using this one for it? – ateebahmed Apr 24 '17 at 11:55
  • Hi, I´m trying to learn how to use the library too, could you please tell me if you have found useful tutorials/examples to use it. I´m working with Gradle just in case, but really any lead would be appreciated. Also, did you manage to get the proxy server option to work? Thank you – Scaramouche Sep 28 '18 at 04:18
  • hey, yes I found it but it's been a while since I wrote the code and can't remember the exact part as it was an android project but I have links here which may help you, I will try to look in my repo and find the code for you though. check out these tests as they show how can you use them. [java-client-library-test-folders](https://github.com/googlemaps/google-maps-services-java/tree/master/src/test/java/com/google/maps) [This](https://mapsplatform.googleblog.com/2015/01/save-time-with-easy-maps-api.html) is a blog post I read [my repo](https://github.com/traffic-analyzer/fyp) – ateebahmed Oct 09 '18 at 19:36

1 Answers1

3

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

Maher Nabil
  • 1,417
  • 1
  • 15
  • 19