Last active
January 15, 2022 14:09
-
-
Save mitchtabian/b8a2dee2804bd1a58c09b045515e430e to your computer and use it in GitHub Desktop.
Calculating Directions with Google Directions API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void calculateDirections(Marker marker){ | |
Log.d(TAG, "calculateDirections: calculating directions."); | |
com.google.maps.model.LatLng destination = new com.google.maps.model.LatLng( | |
marker.getPosition().latitude, | |
marker.getPosition().longitude | |
); | |
DirectionsApiRequest directions = new DirectionsApiRequest(mGeoApiContext); | |
directions.alternatives(true); | |
directions.origin( | |
new com.google.maps.model.LatLng( | |
mUserPosition.getGeo_point().getLatitude(), | |
mUserPosition.getGeo_point().getLongitude() | |
) | |
); | |
Log.d(TAG, "calculateDirections: destination: " + destination.toString()); | |
directions.destination(destination).setCallback(new PendingResult.Callback<DirectionsResult>() { | |
@Override | |
public void onResult(DirectionsResult result) { | |
Log.d(TAG, "calculateDirections: routes: " + result.routes[0].toString()); | |
Log.d(TAG, "calculateDirections: duration: " + result.routes[0].legs[0].duration); | |
Log.d(TAG, "calculateDirections: distance: " + result.routes[0].legs[0].distance); | |
Log.d(TAG, "calculateDirections: geocodedWayPoints: " + result.geocodedWaypoints[0].toString()); | |
} | |
@Override | |
public void onFailure(Throwable e) { | |
Log.e(TAG, "calculateDirections: Failed to get directions: " + e.getMessage() ); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment