Skip to content

Instantly share code, notes, and snippets.

@mitchtabian
Last active January 15, 2022 14:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitchtabian/b8a2dee2804bd1a58c09b045515e430e to your computer and use it in GitHub Desktop.
Save mitchtabian/b8a2dee2804bd1a58c09b045515e430e to your computer and use it in GitHub Desktop.
Calculating Directions with Google Directions API
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