Android provides simple route navigation.
Google map provides a simple Android navigation function.
The starting interface is still this:
Added the following options:
For example, choose to ride:
Wait a moment to load the data.
The alternative route will be provided:
Select the first alternative route.
Select to display on the map. NOTE: If Android 1.5 is used, there is no "display on map" menu entry. The rest are similar.
In fact, the entire application above has very little code written by myself. The selection and display of routes are all functions provided by Google map.
Complete activity code:
Package Proto. GEO;
Import Android. App. activity;
Import Android. App. alertdialog;
Import Android. content. dialoginterface;
Import Android. content. intent;
Import android.net. Uri;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Public class homeactivity extends activity {
Button button;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Button = (button) This. findviewbyid (R. Id. button01 );
Button. setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
Final charsequence [] items = {"Driving", "Walking", "Driving "};
Alertdialog. Builder = new alertdialog. Builder (
Homeactivity. This );
Builder. settitle ("select traffic mode ");
Builder. setitems (items, new dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int item ){
Stringbuilder Params = new stringbuilder (). append ("& dirflg = ");
Switch (item ){
Case 0:
Params. append ("R ");
Break;
Case 1:
Params. append ("W ");
Break;
Case 2:
Params. append ("D ");
Break;
Default:
Break;
}
Getmap (Params. tostring ());
}
});
Alertdialog Alert = builder. Create ();
Alert. Show ();
}
});
}
Protected void getmap (string Params ){
Intent I = new intent (
Intent. action_view,
Uri
. Parse ("http://ditu.google.cn/maps? F = D & source = s_d & saddr = 31.249351, 121.45905 & daddr = 31.186371, 121.489885 & HL = ZH & t = M &"
+ Params ));
I. addflags (intent. flag_activity_new_task
& Intent. flag_activity_exclude_from_recents );
I. setclassname ("com. Google. Android. Apps. Maps ",
"Com. Google. Android. Maps. mapsactivity ");
Startactivity (I );
}
}
Note that the dirflg parameter is used by default. If this parameter is left blank by default, a browser is called to display the map. However, if Google map is called, The last selected method is used, it may be walking or taking a bus. No driving parameters are provided in Google's official mapki documentation. If T or H is used, it does not work because it is a restricted parameter, not a type parameter.
So I thought, if it was me, a D parameter may be set, drive, and the result will take effect, haha.
References:
Http://marshal.easymorse.com/archives/2590