Reputation: 57
I want to embed turn by turn navigation in my android application.please give me a tutorial or an idea for how to do this.thanks in advance.
Upvotes: 0
Views: 6291
Reputation: 1279
I thought that there was no way to embed turn-by-turn instructions into my application, but I was totally wrong. The Google Maps API V2 actually does offer turn-by-turn instructions. Check out Google's documentation.
I used the code given in this library to get basic routing information. I then added the following method to GoogleDirection.java
to return a list of turn-by-turn instructions:
// getInstructions returns a list of step-by-step instruction Strings with HTML formatting
public ArrayList<String> getInstructions(Document doc) {
ArrayList<String > instructions = new ArrayList<String>();
NodeList stepNodes = doc.getElementsByTagName("step");
if (stepNodes.getLength() > 0) {
for (int i = 0; i < stepNodes.getLength(); i++) {
Node currentStepNode = stepNodes.item(i);
NodeList currentStepNodeChildren = currentStepNode.getChildNodes();
Node currentStepInstruction = currentStepNodeChildren.item(getNodeIndex(currentStepNodeChildren, "html_instructions"));
instructions.add(currentStepInstruction.getTextContent());
}
}
return instructions;
}
This approach doesn't offer real-time updates telling you when you've reached a new turn, but it works well and serves my needs. I use my getInstructions
method with an instructionsToString
helper method, which concatenates the instructions with an HTML line break. That code is here if you're interested.
Upvotes: 3
Reputation: 11439
If you are not fixed on using google maps you can use an SDK based on OpenStreetMap (the Wikipedia version of maps)
There are a couple of good SDK providers out there:
As a reference you see the OSM list of frameworks
Upvotes: 3
Reputation: 1113
you can use this code in your Activity:
double latitudeDestination = 52.377028; // or some other location
double longitudeDestination = 4.892421; // or some other location
String requestedMode = "walking" // or bike or car
String mode = "";
if(requestedMode.equals("walking")) {
mode = "&mode=w";
} else if(requestedMode.equals("bike")) {
mode = "&mode=b";
} else if(requestedMode.equals("car")) {
mode = "&mode=c";
}
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(String.format("google.navigation:ll=%s,%s%s", latitudeDestination, longitudeDestination, mode)));
startActivity(intent);
Upvotes: 1