akexorcist / GoogleDirectionLibrary

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

Unspecified amount of waypoints #44

Closed ericywl closed 6 years ago

ericywl commented 6 years ago

Currently the waypoints seem to be hardcoded into GoogleDirection. Is it possible to implement .and( ) multiple times in something like a for loop? So let's say I have 5 waypoints, it will add 5 waypoints into the map. But another case, I may have 7, how do I handle this?

akexorcist commented 6 years ago

Sound cool! Thanks for your request.

I will add

and(List<LatLng> waypoints)

for unspecified amount of waypoints for the next version.


For now, you can implement unspecific amount of waypoints like this.

String serverKey = ...
LatLng origin = ...
LatLng destination = ...
List<LatLng> waypoints = ...

DirectionDestinationRequest destinationRequest = GoogleDirection.withServerKey(serverKey)
        .from(origin);
for (int index = 0; index < waypoints.size(); index++) {
    destinationRequest.and(waypoints.get(index));
}
destinationRequest.to(destination)
        .transportMode(TransportMode.DRIVING)
        .execute(new DirectionCallback() {
            @Override
            public void onDirectionSuccess(Direction direction, String rawBody) {
                // TODO Do something
            }

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

But the code looks ugly.

ericywl commented 6 years ago

Thanks for the help :)

akexorcist commented 6 years ago

I just updated my library to 1.1.1 with unspecified amount of waypoints supported

You can write the code like this

String serverKey = ...
LatLng origin = ...
LatLng destination = ...
List<LatLng> waypoints = ...

DirectionDestinationRequest destinationRequest = GoogleDirection.withServerKey(serverKey)
        .from(origin)
        .and(waypoints)
        .to(destination)
        .transportMode(TransportMode.DRIVING)
        .execute(new DirectionCallback() {
            @Override
            public void onDirectionSuccess(Direction direction, String rawBody) {
                // TODO Do something
            }

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