Obtain Driving Directions with Google Maps API v2
Getting driving directions between two locations is a common requirement for many mapping applications. However, the code you've provided only draws a straight line between the points, rather than providing turn-by-turn directions.
Solution Using Android GoogleDirection Library
To retrieve driving directions, you can utilize the Android GoogleDirectionLibrary, a recently released library by akexorcist. Here's a modified code snippet using the library:
import com.akexorcist.googledirection.DirectionCallback; import com.akexorcist.googledirection.GoogleDirection; import com.akexorcist.googledirection.model.Direction; import com.akexorcist.googledirection.model.Leg; import com.akexorcist.googledirection.model.Route; import com.akexorcist.googledirection.util.DirectionConverter; ... // Replace with your API key String apiKey = "YOUR_API_KEY"; GoogleDirection.withServerKey(apiKey) .from(new LatLng(12.917745600000000000,77.623788300000000000)) .to(new LatLng(12.842056800000000000,7.663096499999940000)) .execute(new DirectionCallback() { @Override public void onDirectionSuccess(Direction direction, String rawBody) { if (direction.isOK()) { Route route = direction.getRouteList().get(0); Leg leg = route.getLegList().get(0); // Draw the path (Polylines) List<latlng> directionPositionList = DirectionConverter.decodePoly(leg.getPolylinePoint()); Polyline line = mMap.addPolyline(new PolylineOptions() .addAll(directionPositionList) .width(5) .color(Color.RED)); // Display turn-by-turn instructions String[] instructions = DirectionConverter.provideInstructionList(leg); for (String instruction : instructions) { Log.d("Direction", instruction); } } else { // Handle error } } @Override public void onDirectionFailure(Throwable t) { // Handle error } });</latlng>
With this modified code, you should be able to obtain driving directions between the two locations, including turn-by-turn instructions.
The above is the detailed content of How to Get Turn-by-Turn Driving Directions Using Google Maps API v2?. For more information, please follow other related articles on the PHP Chinese website!

How to avoid data overwriting and style loss of merged cells when using EasyExcel for template filling? Using EasyExcel for Excel...

How to switch from Java programmers to audio and video development? Learning Paths and Resources Recommendations If you are a Java programmer and are participating in a video project, �...

How to efficiently count the number of node services in MYSQL tree structure in Java? When using MYSQL database, how to count the number of nodes in the tree structure...

How do newcomers choose Java project management tools for backends? Newbie who are just starting to learn back-end development often feel confused about choosing project management tools. Special...

How do Java programmers get started with audio and video development? Learning Resources Recommended As a Java programmer, if you are participating in a video project but are interested in audio and video technology...

In IntelliJ...

Troubleshooting the number of lines doubled when poi-tl draws a double Y-axis line chart. When using poi-tl library to draw a line chart containing two Y-axis, I encountered a strange problem: �...

How to centralize the Entity, Mapper and Service of the business module into the Common module in SpringCloudAlibaba? Using SpringCloud...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment