[Android] Baidu map development (I). Apply for an AK to display a map and solve the problem of displaying a blank grid. androidak

Source: Internet
Author: User
Tags call back

[Android] Baidu map development (I). Apply for an AK to display a map and solve the problem of displaying a blank grid. androidak
Recently I made android Baidu map, but I always encountered problems when I used baidumapapi_v2_3_1.jar and libBaiduMapSDK_v2_3_1.so to display Baidu map-only display the grid without displaying the map, and the network connection and APIKey application are correct, I don't know why it cannot be displayed. There are also many people on the Internet who encounter this problem. Some are incompatible with SDK updates. in addition, many Baidu maps on the Internet use V2.3.1. Later, there was no way to display the map only through baidumapapi_v2_4_1.jar.
Note that when 2.3.1 is used, manager. init ("APIKey", null) is called to initialize the map. 2.4.1 is used to input AK in meta-data of AndroidManifest. xml.
PS: This is just a basic article on developing Baidu map for Android. There are a lot of online materials, but this method is feasible. refer to the method in Guo Shen's first line of Android code, which is v2.3.1. Only the grid can be displayed without a map.

1. Apply for APIKey before development

References:
Official SDK Development GuideHttp://developer.baidu.com/map/sdkandev-14.htm
Baidu map application keyHttp://lbsyun.baidu.com/apiconsole/key
First, register as a Baidu developer.

As shown in.

Click "create Application>", as shown in. The Application List is displayed.

Click "create application" to apply for APIKey. to select the type of blog books we have seen before, we usually select the "for mobile" application type. This option is no longer available here, therefore, I select "for Android" and enter the security code at the same time!

Here you may wonder what the "Security Code" is? It is composed of "digital signature; package name". The digital signature is the SHA1 fingerprint of the keystore when we package the program. by clicking the "Window"> Preferences (Preferences) of Eclipse) -> Android-> Build ", which can be obtained through cmd at the link of the above official document. the package name is the package name corresponding to the application. as follows:
E5: BA: 71: 31: 9D: A3: BF: 92: 8D: 2E: 8F: 3A: 6D: 0A: 93: 5B: 3D: 36: 59: 08; com. example. baidumapshow

Click "Submit" to obtain the AK: QwaNhFQ0ty2QmdYh3Nrr0gQx.


2. Configure the project to call the SDK

Download Android SDK:
Http://developer.baidu.com/map/sdkandev-download.htm
Copy the baidumapapi_v2_4_1.jar file under the libs directory of the created application BaiduMapShow project, create the armeabi directory under the libs directory, and copy the libBaiduMapSDK_v2_4_1.so file, as shown in:

The project package name is com. eample. the baidumapshow and libs directories are used to store third-party Jar packages. The armeabi directory stores the NDK and generates the so file and calls its C/C ++ function library. import the jar package, right-click the project, and choose Properties> Java build path, as shown in.


3. Source Code display Baidu Map

1. activity_main.xml layout File
The layout file loads the Baidu map control MapView, which is a custom control provided by Baidu. Therefore, you must add the complete package name and set the clickable event to true.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/container"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.baidumapshow.MainActivity"    tools:ignore="MergeRootFrame" >    <com.baidu.mapapi.map.MapView        android:id="@+id/map_view"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:clickable="true" /></FrameLayout>
2. MainActivity. java File
Public class MainActivity extends Activity {// BMapManager Object Management map, positioning, and search functions private BMapManager mBMapManager; // MapView map master control private MapView mapView = null; // MapController completes map control private MapController mMapController = null; // MKMapViewListener is used to handle map Event Callback MKMapViewListener mMapListener = null; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState);/*** create object B MapManager and initialization operation * init (APIKey, null) V2.4.1 in V2.3.1 assign an AK in AndroidManifest * Note that the initialization operation is prior to setContentView () */mBMapManager = new BMapManager (getApplication ()); mBMapManager. init (null); setContentView (R. layout. activity_main); mapView = (MapView) findViewById (R. id. map_view); // obtain the map controller to obtain the MapController instance mMapController = mapView. getController (); // sets whether the map responds to the Click Event mMapController. enableClick (true); // sets the map zoom Level 3- 19 level higher information more detailed mMapController. setZoom (16); // display the built-in zoom control mapView. setBuiltInZoomControls (true);/*** get the school longitude and latitude settings map center point */GeoPoint point = new GeoPoint (int) (39.96703*1E6), (int) (116.323772*1E6); mMapController. setCenter (point); mapView. regMapViewListener (mBMapManager, new MKMapViewListener () {/*** call back this interface method when map movement is complete */@ Override public void onMapMoveFinish () {Toast. makeText (MainActivity. this, "Location Figure moving ", Toast. LENGTH_SHORT ). show ();}/*** call back this method after the map is loaded */@ Override public void onMapLoadFinish () {Toast. makeText (MainActivity. this, "map loading", Toast. LENGTH_SHORT ). show ();}/*** after the map completes the animation-carrying operation (for example, animationTo (), this callback is triggered */@ Override public void onMapAnimationFinish () {}/*** when mMapView is called. after getCurrentMap (), this callback will be triggered * can be saved to the storage device */@ Override public void onGetCurrentMap (Bitmap arg0) {}/*** click the marked point on the map Call back this method **/@ Override public void onClickMapPoi (MapPoi arg0) {if (arg0! = Null) {Toast. makeText (MainActivity. this, arg0.strText, Toast. LENGTH_SHORT ). show () ;}}) ;}@ Overrideprotected void onResume () {mapView. onResume (); if (mBMapManager! = Null) {mBMapManager. start () ;}super. onResume () ;}@ Overrideprotected void onDestroy () {mapView. destroy (); if (mBMapManager! = Null) {mBMapManager. destroy (); mBMapManager = null;} super. onDestroy () ;}@ Overrideprotected void onPause () {mapView. onPause (); if (mBMapManager! = Null) {mBMapManager. stop () ;}super. onPause ();}}
3. Declare permissions and APIKey in AndroidManifest. xml
The most important one is to add the APIKey, which is called by mBMapManager. init (null.
<Meta-data
Android: name = "com. baidu. lbsapi. API_KEY"
Android: value = "QwaNhFQ0ty2QmdYh3Nrr0gQx">
</Meta-data>
<? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "com. example. baidumapshow "android: versionCode =" 1 "android: versionName =" 1.0 "> <uses-sdk android: minSdkVersion =" 19 "android: targetSdkVersion =" 19 "/> <! -- Get the network status --> <uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/> <! -- Access Network --> <uses-permission android: name = "android. permission. INTERNET"/> <! -- Get WiFi status --> <uses-permission android: name = "android. permission. ACCESS_WIFI_STATE "/> <uses-permission android: name =" android. permission. CHANGE_WIFI_STATE "/> <! -- Allow programs to write files to external storage, such as SD cards --> <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE "/> <uses-permission android: name =" android. permission. WRITE_SETTINGS "/> <! -- Read the phone status --> <uses-permission android: name = "android. permission. READ_PHONE_STATE "/> <uses-permission android: name =" android. permission. CALL_PHONE "/> <! -- Get the positioning information of the precise GPS chip to receive the satellite, and the positioning accuracy is less than 10 meters --> <uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"/> <! -- Get the incorrect longitude and latitude information by using WiFi or mobile base station --> <uses-permission android: name = "android. permission. ACCESS_COARSE_LOCATION"/> <! -- Get simulated positioning information --> <uses-permission android: name = "android. permission. ACCESS_MOCK_LOCATION "/> <uses-permission android: name =" android. permission. ACCESS_GPS "/> <application android: allowBackup =" true "android: icon =" @ drawable/ic_launcher "android: label =" @ string/app_name "android: theme = "@ style/AppTheme"> <meta-data android: name = "com. baidu. lbsapi. API_KEY "android: value =" QwaNhFQ0ty2QmdYh3Nrr0gQx "> </meta-data> <activity android: name =" com. example. baidumapshow. mainActivity "android: label =" @ string/app_name "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity> </application> </manifest>
4. Shows the running effect.

Finally, I hope the article will be helpful to everyone. This is a basic article I will help my colleagues do a simple research on Baidu map. I am going to talk about Baidu map POI interest search and add annotation functions later! I don't know why the grid is always displayed without a map, but the map can be displayed through V2_4_1.
I guess the reason is that the previously applied APIKey call method can display the map. However, due to the incompatibility of Baidu's new SDK method, the applied APIKey needs to use a new method, therefore, only the above information can be displayed. I don't know if my personal opinions are correct!
(By: EastmountHttp://blog.csdn.net/eastmount/)

References:
1. Chapter 11th of the first line of Android code developed based on Baidu Map
2. xiaanming great god Article Co., http://blog.csdn.net/xiaanming/article/details/11171581.
3. Baidu official documentation and library http://wenku.baidu.com/view/86bab0b3f524ccbff12184bd.html

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.