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.
Asked
Active
Viewed 1,037 times
1
-
Possible duplicate of [How to draw a path on a map using kml file?](http://stackoverflow.com/questions/3109158/how-to-draw-a-path-on-a-map-using-kml-file) – Maveňツ Oct 26 '16 at 08:25
2 Answers
1
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
}
});

Vikash Kumar Verma
- 1,068
- 2
- 14
- 30
-
onDirectionSuccess() will give the direction and rawBody but how i am gonna show in activity? – salman admission Oct 26 '16 at 10:39
-
see updated code and please have a look at the sample given in link – Vikash Kumar Verma Oct 26 '16 at 11:05
0
- Enable Google Maps Directions API in your console
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()); } }); }

orups
- 59
- 5
-
-
replace `compile 'com.akexorcist:googledirectionlibrary:1.0.4'` with `compile 'com.akexorcist:googledirectionlibrary:1.0.5'` to enable logging. [link](http://www.akexorcist.com/2015/12/google-direction-library-for-android-en.html) – orups Jun 17 '17 at 16:18