Some information on the Internet this content is outdated, here is the latest content, if not, please spit groove ...
1) Download Baidu maps Mobile API (ANDROID) Development Kit
To use the Baidu Map API in the Android app, you need to reference the Baidu Map API development package in the project, which contains two files:
: Http://pan.baidu.com/s/1i31enrB
2) Application API Key
Package Name
Format: for example: B7:6c:cf:e2:47:50:9b:3e:34:f7:08:72:f3:ac:f1:be:55:d3:77:fb;com.majianjie.baidumap
Application Address: Http://lbsyun.baidu.com/apiconsole/key
3) Create an Android project directory structure as follows:
4) Add a map control to the layout file (Res/layout/activity_main.xml)
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:o rientation= "vertical" > <com.baidu.mapapi.map.mapview android:id= "@+id/bmapview" android: Layout_width= "Fill_parent" android:layout_height= "fill_parent" android:clickable= "true"/></ Linearlayout>
5) Create activity
Package Com.majianjie.baidumap;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.drawable.drawable;import Android.os.bundle;import Android.widget.toast;import Com.baidu.mapapi.bmapmanager;import Com.baidu.mapapi.mkgenerallistener;import com.baidu.mapapi.map.MKEvent; Import Com.baidu.mapapi.map.mkmapviewlistener;import Com.baidu.mapapi.map.mapcontroller;import Com.baidu.mapapi.map.mappoi;import Com.baidu.mapapi.map.mapview;import Com.baidu.mapapi.map.overlayitem;import Com.baidu.platform.comapi.basestruct.geopoint;import Com.example.baidumap.r;public class MainActivity extends Activity {private Toast mtoast;private bmapmanager mbmapmanager;public Mapview Mmapview = Null;//mapview is the map main control private Ma Pcontroller Mmapcontroller = null;//Complete map control with mapcontroller mkmapviewlistener mmaplistener = null; Mkmapviewlistener for handling Map events callback//show traffic map @overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( savedinstancestate)/** * Before using the Map SDK, you need to initialize the Bmapmanager, this must be initialized in Setcontentview () First */mbmapmanager = new Bmapmanager (this);//The first parameter is API key, the second parameter is common event listener, used to handle the usual network errors, authorization validation errors, etc. You can also not add this callback interface Mbmapmanager.init ("Ldth1svwr7kygaf0atqavwwu", new Mkgenerallistener () {// The callback function called when the authorization error @overridepublic void ongetpermissionstate (int ierror) {if (Ierror = = mkevent.error_permission_denied) { Showtoast ("API key error, please check!") "); }}//Some network status error handling callback function @overridepublic void ongetnetworkstate (int ierror) {if (Ierror = = Mkevent.error_network_connect) { Toast.maketext (Getapplication (), "Your network is wrong!" ", Toast.length_long). Show (); }}); Setcontentview (r.layout.activity_main); Mmapview = (Mapview) Findviewbyid (R.id.bmapview); Mmapcontroller = Mmapview.getcontroller (); * Get Map Controller Mmapcontroller.enableclick (TRUE); * Set whether the map responds to click events. Mmapcontroller.setzoom (12);//* Set map zoom level Mmapview.setbuiltinzoomcontrols (TRUE); * Display built-in zoom control Mmapview.setsatellite (TRUE);//settings shown as satellite map://mmapview.setbuiltinzOomcontrols (TRUE); /** * Use the following code where you want to add overlay, * such as activity's OnCreate () *///Prepare the overlay do you want to add Uble MLAT1 = 39.904508; Double mLon1 = 119.53971899999999; Constructs a GeoPoint with a given latitude and longitude, the unit is the Micro degree (degree * 1E6) GeoPoint p1 = new GeoPoint ((int) (MLAT1 * 1E6), (int) (MLon1 * 1E6)); Prepare overlay image data, repair drawable mark= getresources () according to the situation. Getdrawable (R.drawable.set); Prepare overlay data with Overlayitem overlayitem item1 = new Overlayitem (P1, "Item1", "item1"); Use the Setmarker () method to set the overlay picture, if not set then use the default settings when building Itemizedoverlay//Create Iteminizedoverlay Customitem Izedoverlay itemoverlay = new Customitemizedoverlay (mark, Mmapview); Add Iteminizedoverlay to Mapview mmapview.getoverlays (). Clear (); Mmapview.getoverlays (). Add (Itemoverlay); Now that all preparations are ready, use the following methods to manage overlay. Add overlay, use AddItem when adding overlay in bulk (List<overlayitem>) Higher efficiency itemoverlay.additem (ITEM1); Mmapview.refresh (); Mmapcontroller.setcenter (p1);//set P place as center point Mmapview.regmapviewlistener (Mbmapmanager, New Mkmapviewlistener () { * This interface method is recalled when the map move is complete @overridepublic void Onmapmovefinish () {showtoast ("Map moved! ");} * Map loading complete Callback This interface method @overridepublic void Onmaploadfinish () {showtoast ("Map loaded! ");} * This callback is triggered @overridepublic void Onmapanimationfinish () {}//when the map completes the draw-driven operation (for example: Animationto ()), when the Mmapview.getcurrentmap is called ( ), this callback is triggered to be saved to the storage device @overridepublic void Ongetcurrentmap (Bitmap arg0) {}//* click on the map labeled Point callback this method @overridepublic void Onclickmappoi (Mappoi arg0) {if (arg0! = null) {showtoast (Arg0.strtext);}});} @Overrideprotected void Onresume () {//mapview life cycle is synchronized with activity, call Mapview.onpause () Mmapview.onresume () when activity hangs ; Super.onresume ();} The life cycle of the @Overrideprotected void OnPause () {//mapview is synchronized with the activity and calls Mapview.onpause () Mmapview.onpause () when the activity is suspended; Super.onpause ();} @Overrideprotected void OndesTroy () {//mapview life cycle is synchronized with activity, call Mapview.destroy () Mmapview.destroy () when activity is destroyed;// Exit the app call Bmapmanager's Destroy () method if (Mbmapmanager! = null) {Mbmapmanager.destroy (); mbmapmanager = null;} Super.ondestroy ();} * Show Toast message private void Showtoast (String msg) {if (mtoast = = null) {mtoast = toast.maketext (th IS, MSG, toast.length_short); }else{Mtoast.settext (msg); Mtoast.setduration (Toast.length_short); } mtoast.show (); } }
6) Configure add permissions in Androidmanifest.xml
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android: Name= "Android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name= " Android.permission.INTERNET "/> <uses-permission android:name=" android.permission.WRITE_EXTERNAL_ STORAGE "/> <uses-permission android:name=" Android.permission.ACCESS_WIFI_STATE "/> < Uses-permission android:name= "Android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:name= " Android.permission.READ_PHONE_STATE "/>
7) There is also a file that is custom, inherited from:Itemizedoverlay (Baidu map, not Android own)
Overlay Introduction
overlay are often translated as "layers" or "overlays". So what do you call a cover for a map? "all content superimposed or overlaid onto the map is called a map overlay., such as callouts, vector graphic elements (including: Polylines and polygons, and circles), positioning icons, and so on. Overlays have their own geographic coordinates, and when you drag or zoom the map, they move accordingly. "
in order to let us have a further understanding of the overlay, we will then use the following figure to intuitively understand it. The red icons marked in the figure A, B,..., J is one of the forms of overlay. If you understand the concept of layers in Photoshop, this should not be difficult to understand. we can understand the relationship between a map, a layer, and a marker on a layer: those red tags are not part of the map, all of which are attached to the layer, and then the layer is masked on the map.
Baidu Map API provides several overlay
The Baidu Map API provides several overlay (overlays):
1) Overlay: It is the abstract base class of all the coverings, all the overlays inherit this kind of method, realize the user custom layer display;
2) Mylocationoverlay: a responsibleoverlay showing the user's current location;
3) Itemizedoverlay: It contains a list of Overlayitem, equivalent tooverlay of a component bar, and by inheriting this class, a set of points of interest is displayed on the map;
4) Poioverlay:Local Search Layerto provide a location search service for a particular region, such as a search for "Parks" in Beijing, which shows the park on a map by this layer;
5) Routeoverlay:walking, driving navigation route layer, the route and key points of the walking and driving plan are shown on the map;
6) Transitoverlay:Bus transfer route layer, the route and transfer location of the public transport plan for a particular area are displayed on the map.
The following file is: Customitemizedoverlay.java online Some content is relatively early, Baidu Map API has abandoned the use , here is the latest, detailed: http://developer.baidu.com/map/ sdkandev-6.htm#. E7. Ae.80.e4.bb.8b3
Package Com.majianjie.baidumap;import Android.graphics.drawable.drawable;import Com.baidu.mapapi.map.itemizedoverlay;import Com.baidu.mapapi.map.mapview;import Com.baidu.mapapi.map.OverlayItem ; Import Com.baidu.platform.comapi.basestruct.geopoint;public class Customitemizedoverlay extends Itemizedoverlay <OverlayItem> { //Itemizedoverlay public customitemizedoverlay constructed with Mapview (drawable Mark,mapview Mapview) { super (Mark,mapview); } protected Boolean onTap (int index) { //In this processing item click event System.out.println ("Item OnTap:" +index); return true; } public boolean OnTap (GeoPoint pt, Mapview mapview) { //In this process Mapview click events, when returning True Super.ontap (Pt,mapview); return false; } }
Operation diagram:
Basic on these, Baidu map API content is very rich, will be added after, please continue to pay attention! Thank you! If something is wrong, please spit the groove ...