One Application API Key
before using Baidu Map, we must apply for a Baidu Map API key, application address Http://lbsyun.baidu.com/apiconsole/key, self-registration of a Baidu account, will soon be able to apply for, such as
Create an App
Key type Select "For mobile", the security code is the certificate thumbprint (SHA1) value of the Android signing certificate + ";" + your application package name, so when you have configured the API key you can not change the package name of the application, if you change the package name we need to reconfigure API key, let's show you how to get a digital signature
We know that we develop the Android program needs to be signed to him, if no signature is not allowed to be installed on the phone or the simulator, then you will have doubts, my usual development of the application is not signed, how can be run directly on the simulator or mobile phone, In fact, ADT will automatically use the debug key to sign the application, of course, you can also create your own key , directly with the Eclipse visual creation on the line, very convenient, here I do not introduce
Here we use Debug.keystore to generate android signing Certificate's certificate thumbprint (SHA1) value , can be viewed directly in Eclipse: Android-----winows---preferance. As shown :
Then we use the Keytool tool to obtain the SHA1 value of the signing certificate, keytool-list-keystore the DOS input C:\Users\bds\.android\ Debug.keystore Red part of the path of Debug.keystore, Next will you enter the KeyStore password, the default input "Android", this way we can obtain the certificate fingerprint (SHA1), such as
The red box is what we need the certificate fingerprint (SHA1) value of the Android signature certificate , and then we copy it, enter it into the Security Code input box of the API key, and then separate it with your application package name, such as XX:XX:B2:EF : 94:ff:ee:cc:3c:5a:29;com.example.baidumapdemo So we've configured the API key
Second , download Baidu map API Library
To use Baidu Map API in Android app, you need to quote Baidu Map API Development Kit, http://developer.baidu.com/map/sdkandev-download.htm, download Android SDK in Project v3.1.0Lib Library is on the line.
New Android Project Baidumapdemo, then add Baidu Map API library to the project, such as
2) Add the required permissions
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android: Name= "Android.permission.INTERNET"/> <uses-permission android:name= " Com.android.launcher.permission.READ_SETTINGS "/> <uses-permission android:name=" Android.permission.WAKE_LOCK "/> <uses-permission android:name=" Android.permission.CHANGE_WIFI_STATE "/ > <uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/> <uses-permission Android:name= "Android.permission.GET_TASKS"/> <uses-permission android:name= "Android.permission.WRITE _external_storage "/> <uses-permission android:name=" Android.permission.WRITE_SETTINGS "/>
Add the Baidu map control to the layout file,
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent "> < Com.baidu.mapapi.map.MapView android:id= "@+id/bmapview" android:layout_width= "Fill_parent" Android : layout_height= "fill_parent" android:clickable= "true"/></relativelayout>
Activity interface code, comments are very detailed, I believe you can understand
Package Com.akwl.app.activity;import Java.util.list;import Android.app.alertdialog;import Android.content.dialoginterface;import Android.content.intent;import Android.os.asynctask;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import android.widget.Toast; Import Com.akwl.app.r;import Com.akwl.app.bean.cmschannel;import Com.akwl.app.util.queryresult;import Com.akwl.app.util.rcpparams;import Com.baidu.mapapi.sdkinitializer;import Com.baidu.mapapi.map.baidumap;import Com.baidu.mapapi.map.mapview;public class Ruralsceneryactivity extends Appbaseactivity implementsonclicklistener { Mapview Mapview; Baidu map @overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);// Before using the SDK components to initialize the context information, pass in applicationcontext//Note that the method is implemented before Setcontentview the method Sdkinitializer.initialize ( Getapplicationcontext ()); Setcontentview (r.layout.activity_ruralscenery);//Get a reference to a map control Mapview = (Mapview) Findviewbyid (R.id.bmapview); Initview ();} Private void Initview () {Baidumap mbaidumap = Mapview.getmap ();//General Map//mbaidumap.setmaptype (Baidumap.map_type_normal); Satellite map Mbaidumap.setmaptype (baidumap.map_type_satellite);} @Overrideprotected void OnDestroy () {Super.ondestroy ();//Mmapview.ondestroy () is executed when activity executes OnDestroy, Implement Map Lifecycle Management Mapview.ondestroy ();} @Overridepublic void onbackpressed () {super.onbackpressed (); Mapview.ondestroy (); Finish ();}}
- mapview is our map control, Mapview has a life cycle synchronized with the activity, such as Onresume (), onpause (), onrestoreinstancestate (Bundle State), We can get the map controller Mapcontroller through the Getcontroller () method, which can be used to control and drive panning and zooming, etc.
- mapview has two interfaces to register, respectively is Mkmaptouchlistener (map click event Listener), Mkmapviewlistener (map Listener) above the demo I gave Mapview registered Mkmapviewlistener, also simple implementation of the inside of several methods, Next I will take everyone to understand the method in detail, I hope that everyone will follow my blog
Baidu Map development Android app key application