I. Description of the problem
Using the Baidu map implementation as shown in the application, first automatically locate the current my starting position (small dot position), and follow the mobile constantly automatically locate my current position
Baidu API Different versions of the use will be some differences, in this case, add Lib as follows:
Ii. Preparation of MyApplication class
public class MyApplication extends application {static MyApplication MyApplication;
Bmapmanager Mbmapmanager = null;
String Mstrkey = "7zfurcox1g3oz8tkutngm4so"; Boolean m_bkeyright = true;
Authorization key is correct, verify through private list<activity> activitylist = new linkedlist<activity> (); @Override public void OnCreate () {myapplication = this; Initenginemanager (this); Super.oncreate ();} public void Initengi
Nemanager {if (Mbmapmanager = = null) {Mbmapmanager = new Bmapmanager (context); Toast.maketext (Myapplication.getinstance (). Getapplicationcontext (), "Bmapmanager initialization sucsuc!", Toast.LENGTH_LONG).
Show (); } if (!mbmapmanager.init (Mstrkey, New Mygenerallistener ())) {Toast.maketext (Myapplication.getinstance ()).
Getapplicationcontext (), "Bmapmanager initialization error!", Toast.length_long). Show (); The public static MyApplication getinstance () {if (MyApplication = null) {MyApplication = new MyApplication ();} retur
n MyApplication; } public void addactivity (activity activity) {activityList.add (activity);
public void exit () {for (activity activity:activitylist) {activity.finish ()}
System.exit (0); }//Common event listening, used to handle common network errors, authorization validation errors, and other static class Mygenerallistener implements Mkgenerallistener {public void Ongetnetworksta te (int ierror) {if (Ierror = = Mkevent.error_network_connect) {Toast.maketext (Myapplication.getinstance (). Getapplicationcontext (), "Your network is wrong!" ", Toast.length_long). Show ();
else if (Ierror = = Mkevent.error_network_data) {Toast.maketext (Myapplication.getinstance (). Getapplicationcontext (), "Enter the correct search criteria!"
", Toast.length_long). Show ();
// ...
} public void ongetpermissionstate (int ierror) {if (Ierror = = mkevent.error_permission_denied) {//Authorization key error: Toast.maketext (Myapplication.getinstance (). Getapplicationcontext (), "Please enter the correct authorization key!
", Toast.length_long). Show ();
Myapplication.getinstance (). M_bkeyright = false; }
}
}
}
Third, write the main program mainactivity, show the current location
Main program Mainactity:
public class Mainactivity extends activity {public MyApplication app; static Mapview mmapview = null; public Mkmapviewli
Stener mmaplistener = null;
Mylocationoverlay mylocationoverlay = null;
Locating related locationclient mlocclient;
Public Notifylister mnotifyer = null;
Public Mylocationlistenner MyListener = new Mylocationlistenner ();
Locationdata locdata = null;
Private Mapcontroller Mmapcontroller = null;
Static Mksearch Mkserach; Handler Mhandler = new Handler () {public void Handlemessage (Android.os.Message msg) {Toast.maketext (Mainactivity.this, "
Msg: "+ msg.what, Toast.length_short). Show ();};
Static TextView showaddr; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_main);
Showaddr = (TextView) Findviewbyid (R.ID.SHOWADDR);
Myicon mi = new Myicon (this); At the center point of the screen, add my icon GetWindow (). Addcontentview (MI, New Layoutparams (Layoutparams.fill_parent, layoutparams.fill_parent)
); MyIcon2 m2 = new MyIcon2 (this);
GetWindow (). Addcontentview (M2, new Layoutparams (Layoutparams.fill_parent, layoutparams.fill_parent));
Mmapview = (Mapview) Findviewbyid (R.id.bmapsview);
Mmapcontroller = Mmapview.getcontroller ();
Initmapview ();
App = Myapplication.getinstance ();
Mlocclient = new Locationclient (this);
Mlocclient.registerlocationlistener (MyListener);
Search initialization Mkserach = new Mksearch (); Mkserach.init (App.mbmapmanager, New Mksearchlistener () {@Override public void Ongetwalkingrouteresult ( Mkwalkingrouteresult arg0, int arg1) {//TODO auto-generated method stub} @Override public void Ongettransitrouteresult ( Mktransitrouteresult arg0, int arg1) {//TODO auto-generated method stub} @Override public void Ongetsuggestionresult (MK Suggestionresult arg0, int arg1) {//TODO auto-generated method stub} @Override public void Ongetpoiresult (Mkpoiresult a rg0, int arg1, int arg2) {//TODO auto-generated method stub} @Override public void Ongetpoidetailsearchresult (int arg0, int arg1) {//TODO auto-generated Method stub} @Override public void Ongetdrivingrouteresult (Mkdrivingrouteresult arg0, int arg1) {//TODO auto-generated Method stub} @Override public void Ongetbusdetailresult (Mkbuslineresult arg0, int arg1) {//TODO auto-generated method
stub} @Override public void Ongetaddrresult (mkaddrinfo info, int arg1) {showaddr.settext (INFO.STRADDR);}});
Set map related locationclientoption option = new Locationclientoption ();
Option.setopengps (TRUE);
Option.setcoortype ("Bd09ll");
Option.setscanspan (300000);
mlocclient.setlocoption (option);
Mlocclient.start ();
Mmapview.getcontroller (). Setzoom (16);
Mmapview.getcontroller (). Enableclick (True);
Mmapview.displayzoomcontrols (TRUE);
Mmaplistener = new Mkmapviewlistener () {public void Onmapmovefinish () {} public void Onclickmappoi (Mappoi mappoiinfo) {
TODO auto-generated Method Stub String title = "";
if (mappoiinfo!= null) {title = Mappoiinfo.strtext;
Toast.maketext (Mainactivity.this, title, Toast.length_short). Show ();
}
}
}; Mmapview.regmapvIewlistener (Myapplication.getinstance (). Mbmapmanager, Mmaplistener);
Mylocationoverlay = new Mylocationoverlay (Mmapview);
Locdata = new Locationdata ();
Mylocationoverlay.setdata (Locdata);
Mmapview.getoverlays (). Add (Mylocationoverlay);
Mylocationoverlay.enablecompass ();
Mmapview.refresh (); "private void Initmapview () {mmapview.setlongclickable (true);}/** * Listener function, new location, formatted as string, output to screen/public class Myloca Tionlistenner implements Bdlocationlistener {public void onreceivelocation (bdlocation location) {if (location = null) r
Eturn;
Locdata.latitude = Location.getlatitude ();
Locdata.longitude = Location.getlongitude ();
Locdata.direction = 2.0f;
Locdata.accuracy = Location.getradius ();
Locdata.direction = Location.getderect ();
LOG.D ("Loctest", String.Format ("Before:lat:%f Lon:%f", Location.getlatitude (), Location.getlongitude ());
Mylocationoverlay.setdata (Locdata);
Mmapview.refresh (); Mmapcontroller. Animateto (new Geopoint (int) (Locdata.latitude * 1e6), (int) (Locdata.longitude * 1e6)), Mhandler. Obtainmessage (1));
public void Onreceivepoi (Bdlocation poilocation) {if (poilocation = = null) {return;}}} public class Notifylister extends Bdnotifylistener {public void onnotify (bdlocation mlocation, float distance) {}} @Ove Rride protected void OnPause () {mmapview.onpause (); Super.onpause ();} @Override protected void Onresume () {Mmapview.onr
Esume ();
Super.onresume ();
} @Override protected void Onsaveinstancestate (Bundle outstate) {super.onsaveinstancestate (outstate);
Mmapview.onsaveinstancestate (outstate); } @Override protected void Onrestoreinstancestate (Bundle savedinstancestate) {super.onrestoreinstancestate (
Savedinstancestate);
Mmapview.onrestoreinstancestate (savedinstancestate); public static void GetPosition (Geopoint g) {Mkserach.reversegeocode (g); Showaddr.settext ("Get location ...");}
XML layout:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http:// Schemas.android.com/tools "
android:layout_width=" fill_parent "
android:layout_height=" Fill_parent
" android:orientation= "vertical"
>
<textview
android:id= "@+id/showaddr"
Center "
android:layout_width=" fill_parent "
android:layout_height=" wrap_content "
android:text=" Select Location "
android:textsize=" 20DP "
/>
<com.baidu.mapapi.map.mapview
android:id=" @+id/ Bmapsview "
android:layout_width=" fill_parent "
android:layout_height=" Fill_parent "
android: Clickable= "true"/>
</LinearLayout>
Third, drawing graphics
Myicon OnDraw implements the icon to draw the center point, MyIcon2 Draw the indicator icon, as shown in the figure
public class Myicon extends View {public static int w; public static int h; public stat
IC Bitmap Mbitmap; Public Myicon {Super (context); Mbitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.me)
;
@Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas); w = this.getwidth ()/2-mbitmap.getwidth ()/2;
h = this.getheight ()/2-mbitmap.getheight ()/2;
Canvas.drawbitmap (Mbitmap, W, h, NULL); The public class MyIcon2 extends view{the public static int w; public static int h; private Bitmap mbitmap. Public MyIcon2 (Co
ntext context) {super (context); Mbitmap = Bitmapfactory.decoderesource (Getresources (), R.drawable.jiewo); @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas); w = this.getwidth ()/2-mbitmap.getwidth ()/2; h = (THIS.G
Etheight ()/2-mbitmap.getheight ()/2)-(MyIcon.mBitmap.getHeight ()/2);
Canvas.drawbitmap (Mbitmap, W, h, NULL); }
}
The above is a small set to share the Android Baidu positioning navigation based on Baidu Map mobile acquisition location and automatic positioning of the relevant knowledge, I hope to help.