akexorcist / GoogleDirectionLibrary

[Android] Library for Google Direction API for Google Maps Android API v2
Apache License 2.0
571 stars 174 forks source link

Route not getting displayed #25

Closed gamebusterz closed 6 years ago

gamebusterz commented 7 years ago

I have added the Gradle dependency, and followed the documentation and I can see in the Console that my Google Maps Directions API is being used. But there is no path that is being displayed between the origin and the destination.

If it helps, I am already adding an Origin and a Destination marker to the app, so I hope this library doesn't try to add those markers as well, and might be causing some problem, but still there should've been a visible path.

GoogleDirection.withServerKey("MY_API_KEY")
                        .from(latLngOrigin)
                        .to(latLngDestination)
                        .transportMode(TransportMode.DRIVING)
                        .unit(Unit.IMPERIAL)
                        .avoid(AvoidType.FERRIES)
                        .execute(new DirectionCallback() {
                            @Override
                            public void onDirectionSuccess(Direction direction, String rawBody) {
                                if(direction.isOK()) {
                                    // TODO
                                } else {
                                    // TODO
                                }
                            }

                            @Override
                            public void onDirectionFailure(Throwable t) {
                                // TODO
                            }
                        });

Android 6.0
Android Studio 2.12

gamebusterz commented 7 years ago

Any help please ?

altaf2892 commented 7 years ago

Don't forget to enable the Google Maps Direction API before get a new server key.

ToniNgethe commented 7 years ago

Any one solved this? My route aint displaying

gamebusterz commented 7 years ago

No, still doesn't work for me. My Direction API was already enabled.

sagar-bhatt commented 7 years ago

@gamebusterz,

You are missing a key component to show the route. Try doing the following:

if(direction.isOK()) {
    try{
        List<Step> stepList = direction.getRouteList().get(0).getLegList().get(0).getStepList();
        ArrayList<PolylineOptions> polylineOptionList = DirectionConverter.createTransitPolyline(getActivity(), stepList, 5, routeColor, 3, Color.BLUE);
        for (PolylineOptions polylineOption : polylineOptionList) {
            mMap.addPolyline(polylineOption);
        }
    }catch (Exception e){
        e.printStackTrace();
    }
}

The snippet in the try block will draw the polyline between source and destination. Hope this helps.

Best, Sagar