Android Baidu Map Development (a)---application API key and display Baidu map in the project

Source: Internet
Author: User
Tags call back certificate fingerprint sha1

reprint Please specify address http://blog.csdn.net/xiaanming/article/details/11171581

Recently I want to study the map, originally wanted to study Google map, but the application API key than the pit dad, so from the Baidu map to start, in fact, they are almost identical to the use of methods, this article will lead you in their own Android project to increase the function of Baidu map, Next I will write a series of articles about Baidu map, welcome to the time to pay attention!


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-registrationof a Baidu account, very quickly can apply to, For example with


    • Click "Create Key", the system will generate the key for our own initiative, of course, we are using Keys must also be configured, click "Settings" for example to



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, assuming the replacement of the package name we must To configure the API key again, let's describe how to get a digital signature

We know that we have developed the Android program needs to be signed to him, assuming that no signature is not agreed to be installed on the phone or simulator, then you will have doubts, my usual development of the application is not signed, how can be executed directly on the simulator or mobile phone, In fact, ADT will voluntarily use the debug key to sign the application, of course, you can also create a self-owned key , directly with the Eclipse visual creation can be, very convenient, here I do not introduce

    • Here we use Debug.keystore to generate Certificate Thumbprint (SHA1) value for Android signing certificate , ability to view directly in Eclipse: Android-----winows---preferance. For example :


then we use the Keytool tool to get The SHA1 value of the signing certificate, in DOS input keytool-list-keystore C:\Users\bds\.android\debug.keystore The red part is the path of the Debug.keystore, then you enter the key store password, the default input "Android", this way we can obtain the certificate thumbprint (SHA1), for example, to


The red box is what we need the certificate fingerprint (SHA1) value of the Android signature Certificate , and then we copy it and enter it into the Security Code input box of the API key. Separated by semicolons plus your application package name, such as 02:5c:80:25:b2:8f:6f:60:54:b9:f4:b2:ef:94:ff:ee:cc:3c:5a:29;com.example.baidumapdemo In this way, we've configured the API key.


Second , download Baidu map API Library

To use the Baidu map API in Android apps, you need to refer to the Baidu Map API Development Kit in Project, http://developer.baidu.com/map/sdkandev-download.htm, download Android SDKv2.1.3 Lib library.


Third, in the Android project to cite Baidu map

    • New Android Project Baidumapdemo, then add the Baidu Map API library to project, for example


    • Add Baidu Map control to the 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>

    • Activity interface code, staring very specific, I believe you can understand

Package Com.example.baidumapdemo;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;/** * Mapview is the map master control */private Mapview Mmapview = null;/** * with Mapcontroller complete map control */priva Te Mapcontroller mmapcontroller = null;/** * mkmapviewlistener for handling map event callbacks */mkmapviewlistener Mmaplistener = null; @Overri deprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);/** * Before using the Map SDK, initialize the Bmapmanager first, which must be initialized in Setcontentview () */mbmapmanager = new Bmapmanager (this);//The first parameter is API key,// The second parameter is the frequent use of event snooping toThe usual network errors, authorization validation errors, etc., you can also not add this callback interface Mbmapmanager.init ("7ae13368159d6a513eaa7a17b9413b4b", 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);        /** * Get Map Controller */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 controls */Mmapview.setbuiltinzoomcontrols (TRUE); /** * Save precision and latitude classes, */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 () {/** * map will call back this interface method when it is finished */@Overr idepublic void Onmapmovefinish () {showtoast ("Map moved! ");} /** * Map loading completed callback This interface method */@Overridepublic void Onmaploadfinish () {showtoast ("Map loaded! ");} /** * This callback is triggered when the map is finished (e.g., Animationto ()), */@Overridepublic void Onmapanimationfinish () {}/** * when called Mmapview.getcurren TMap (), this callback will be triggered * can be saved to the storage device */@Overridepublic void Ongetcurrentmap (Bitmap arg0) {}/** * Click on the map marked point callback This method * */@Overridepu Blic 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 () {/The life cycle of the/mapview is synchronized with the activity, call Mapview.destroy () Mmapview.destroy () when the activity is destroyed,//exit the Destroy () method of the application call Bmapmanager 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, MSG, toast.length_short);              }else{Mtoast.settext (msg);        Mtoast.setduration (Toast.length_short);      } mtoast.show (); } }

  1. Bmapmanager is the engine class of the map, this must be instantiated before the Setcontentview method, we need to use its method init (String Strkey,mkgenerallistener Listener) to add the API key, Mkgenerallistener This interface returns the network status, authorization verification and other results, we need to implement the interface to handle the corresponding event
  2. Mapview is our map control, and the Mapview has a life cycle that is synchronized with the activity, such as Onresume (), onPause (), onrestoreinstancestate (Bundle state), destroy (), etc., we can obtain the map controller Mapcontroller through the Getcontroller () method, which can be used to control and drive panning and zooming, etc.
  3. Mapview has two interfaces to register, each is Mkmaptouchlistener (map click event Listener), Mkmapviewlistener (map Listener) above the demo I gave Mapview register Mkmapviewlistener , but also simple implementation of the inside of a few methods, and then I will take everyone to understand the specific method, I hope that the time you focus on my blog
    • Of course, we must also increase the corresponding permissions before the program executes
  <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 "/>

    • Execution results



Well, today's commentary to this end, a friend in doubt please leave a message below. After that will continue to introduce the use of Baidu map, you are welcome to pay attention





Android Baidu Map Development (a)---application API key and display Baidu map in the project

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.