Reputation: 47
I have two location points in my variable and I want to show a map with the route between these two locations. I just want a Google map with a route between the locations.
Upvotes: 1
Views: 1064
Reputation: 99
you could transfer that code to a map listener EG.
@Override
public void onMapClick(LatLng latLng) {
GoogleDirectionConfiguration.getInstance().setLogEnabled(true);
$key = getResources().getString(R.string.google_maps_key;
GoogleDirection.withServerKey($key))
.from(mCurrLocationMarker.getPosition())
.to(latLng)
.transportMode(TransportMode.DRIVING)
.transitMode(TransitMode.BUS)
.unit(Unit.METRIC)
.execute(new DirectionCallback() {
@Override
public void onDirectionSuccess(Direction direction, String rawBody) {
Log.d("GoogleDirection", "Response Direction Status: " + direction.toString()+"\n"+rawBody);
if(direction.isOK()) {
// Do something
Route route = direction.getRouteList().get(0);
Leg leg = route.getLegList().get(0);
ArrayList<LatLng> directionPositionList = leg.getDirectionPoint();
PolylineOptions polylineOptions = DirectionConverter.createPolyline(getApplication(), directionPositionList, 5, Color.RED);
mMap.addPolyline(polylineOptions);
} else {
// Do something
}
}
@Override
public void onDirectionFailure(Throwable t) {
// Do something
Log.e("GoogleDirection", "Response Direction Status: " + t.getMessage()+"\n"+t.getCause());
}
});
}
Upvotes: 0
Reputation: 1078
you can use this Android-GoogleDirectionLibrary
To add this library add dependency in gradle as
compile 'com.akexorcist:googledirectionlibrary:1.0.4'
Sample code
GoogleDirection.withServerKey("YOUR_SERVER_API_KEY")
.from(new LatLng(37.7681994, -122.444538))
.to(new LatLng(37.7749003,-122.4034934))
.avoid(AvoidType.FERRIES)
.avoid(AvoidType.HIGHWAYS)
.execute(new DirectionCallback() {
@Override
public void onDirectionSuccess(Direction direction, String rawBody) {
if(direction.isOK()) {
// Do something
Snackbar.make(btnRequestDirection, "Success with status : " + direction.getStatus(), Snackbar.LENGTH_SHORT).show();
googleMap.addMarker(new MarkerOptions().position(origin));
googleMap.addMarker(new MarkerOptions().position(destination));
ArrayList<LatLng> directionPositionList = direction.getRouteList().get(0).getLegList().get(0).getDirectionPoint();
googleMap.addPolyline(DirectionConverter.createPolyline(this, directionPositionList, 5, Color.RED));
}
} else {
// Do something
}
}
@Override
public void onDirectionFailure(Throwable t) {
// Do something
}
});
Upvotes: 1