Effect:
API key Application Address: Http://lbsyun.baidu.com/apiconsole/key
Application notes:
Security Code: SHA1 Fingerprint value +;+ package name below interface
Example: 72dem98dme;com.map
、、、、、、、、、、、 directory Structure 、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
Code:
Mainactivity.java
Package Com.mybdmap;import Android.app.activity;import Android.graphics.bitmap;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.platform.comapi.basestruct.geopoint;public class Mainactivity extends Activity {private Toast mtoast; Private Bmapmanager mbmapmanager;private Mapview Mmapview = null;//Complete map control with Mapcontroller private Mapcontroller Mmapcontroller = null;//mkmapviewlistener for handling map event callbacks Mkmapviewlistener Mmaplistener = null; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);//You need to initialize Bmapmanager before using the Map SDK, This must be initialized at Setcontentview () First mbmapmanager = new Bmapmanager (Getapplicationcontext ());//The first parameter is API key,//the second parameter is the common event listener, To handle the usual network errors, authorization validation errors, etc., you can also not add this callback interface MbmapManager.init ("Replace with your own AK", New Mkgenerallistener () {//] authorization error when calling the callback function @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 ();//sets whether the map responds to click events. Mmapcontroller.enableclick (TRUE);//set map zoom level Mmapcontroller.setzoom (12) ;//display built-in zoom control Mmapview.setbuiltinzoomcontrols (TRUE);//The class that holds the precision and latitude, GeoPoint p = new GeoPoint ((int) (22.547923 * 1E6), (int) ( 114.067368 * 1E6));//set P place as center point Mmapcontroller.setcenter (p); Mmapview.regmapviewlistener (Mbmapmanager, new Mkmapviewlistener () {////The map move will be callback this interface method @overridepublic void Onmapmovefinish () {//Showtoast ("Map moved finished! ");} The map has finished loading callback this interface method @overridepublic void Onmaploadfinish () {ShowToast ("Map loading completed!") ");} /** * This callback is triggered when the map completes the draw-driven operation (e.g., Animationto ()) */@Overridepublic void Onmapanimationfinish () {}/** * when called Mmapview.getcurrentmap (), this callback is triggered to be saved to the storage device */@Overridepublic void Ongetcurrentmap (Bitmap arg0) {}/** * Click on the marked point on the map to callback this method * */@Overridepublic void Onclickmappoi (Mappoi arg0) {if (arg0! = null) {showtoast (Arg0.strtext);}});} The life cycle of @Overrideprotected void Onresume () {//Mapview is synchronized with the activity and calls 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 ();} The life cycle of @Overrideprotected void OnDestroy () {//Mapview is synchronized with the activity and calls Mapview.destroy () Mmapview.destroy () when activity is destroyed ;//Exit Application call Bmapmanager's Destroy () method if (Mbmapmanager! = null) {Mbmapmanager.destroy (); mbmapmanager = null;} Super.ondestroy ();} /** * Show Toast message * * @param msg */private void Showtoast (String msg) {if (mtoast = = null) {Mtoast = Toast.maketext (This, m SG, toast.length_short);} else {MTOast.settext (msg); Mtoast.setduration (Toast.length_short);} Mtoast.show ();}}
Layout file:
<?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>
Permissions:
<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 "/> <uses-permission android:name=" android.permission.WRITE_ SETTINGS "/>
The required jar+so:http://download.csdn.net/detail/u013425527/8265569
The development of Baidu map in Rabbit--android