1) download Baidu map mobile API (Android) Development Kit
To use Baidu map API in Android applications, You need to reference Baidu map API development kit in the project. This development kit contains two files: baidumapapi. jar and libbmapapiengine. So. : Http://dev.baidu.com/wiki/imap/index.php? Title = Android % E5 % B9 % B3 % E5 % 8f % B0/% E7 % 9B % B8 % E5 % 85% B3 % E4 % B8 % 8B % E8 % BD
2) apply for an API key
Similar to Google map API, you also need to obtain the corresponding API key before using Baidu map API. The Baidu map API key is associated with your Baidu account. Therefore, you must have a Baidu account before obtaining the API key. The key is related to the program name you reference the API.
The application for Baidu API key is much simpler than Google's. In fact, as long as you have a Baidu account, you should be able to complete the application for API key within 30 seconds. Application address: http://dev.baidu.com/wiki/static/imap/key/
3) create an android Project
Here, we need to emphasize that Baidu map mobile API supports Android 1.5 and later systems. Therefore, the project we created should be based on Android SDK 1.5 and later.
After the project is created, set the baidumapapi. jar and libbmapapiengine. so copy them to the root directory of the project and the libs/armeabi directory, select "add jars" from Project Properties> JAVA build path> libraries, and select "baidumapapi. jar to use the Baidu map API in the application. Shows the complete directory structure of the project:
4) Add a map control to the layout file.(RES/layout/Main. XML)
[XHTML]View plaincopy
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
- Android: Orientation = "vertical"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- >
- <Com. Baidu. mapapi. mapview Android: Id = "@ + ID/map_view"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- Android: clickable = "true"
- />
- </Linearlayout>
5) Create an activity to inherit from Com. Baidu. mapapi. mapactivity
[Java]View plaincopy
- Package com. Liufeng. baidumap;
- Import Android. Graphics. drawable. drawable;
- Import Android. OS. Bundle;
- Import com. Baidu. mapapi. bmapmanager;
- Import com. Baidu. mapapi. geopoint;
- Import com. Baidu. mapapi. mapactivity;
- Import com. Baidu. mapapi. mapcontroller;
- Import com. Baidu. mapapi. mapview;
- Public class mainactivity extends mapactivity {
- Private bmapmanager mapmanager;
- Private mapview;
- Private mapcontroller;
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- // Initialize mapactivity
- Mapmanager = new bmapmanager (getapplication ());
- // The first parameter of the init method must be filled with the applied API key.
- Mapmanager. INIT ("285b450ebab2a92293e85502150ada7f03c777c4", null );
- Super. initmapactivity (mapmanager );
- Mapview = (mapview) findviewbyid (R. Id. map_view );
- // Set map mode to traffic map
- Mapview. settraffic (true );
- // Set to enable the built-in zoom control
- Mapview. setbuiltinzoomcontrols (true );
- // Construct a geopoint (latitude and longitude) with the given latitude and longitude)
- Geopoint point = new geopoint (INT) (47.118440*1e6), (INT) (87.493147*1e6 ));
- // Create a tag maker
- Drawable marker = This. getresources (). getdrawable (R. drawable. iconmarka );
- // Define location and boundary for maker
- Marker. setbounds (0, 0, marker. getintrinsicwidth (), marker. getintrinsicheight ());
- // Obtain the map controller object for controlling mapview
- Mapcontroller = mapview. getcontroller ();
- // Set the map center
- Mapcontroller. setcenter (point );
- // Set the default zoom level of the map
- Mapcontroller. setzoom (12 );
- }
- @ Override
- Protected Boolean isroutedisplayed (){
- Return false;
- }
- @ Override
- Protected void ondestroy (){
- If (mapmanager! = NULL ){
- Mapmanager. Destroy ();
- Mapmanager = NULL;
- }
- Super. ondestroy ();
- }
- @ Override
- Protected void onpause (){
- If (mapmanager! = NULL ){
- Mapmanager. Stop ();
- }
- Super. onpause ();
- }
- @ Override
- Protected void onresume (){
- If (mapmanager! = NULL ){
- Mapmanager. Start ();
- }
- Super. onresume ();
- }
- }
- 6) Configure in androidmanifest. xml
[XHTML]View plaincopy
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
- Package = "com. Liufeng. baidumap"
- Android: versioncode = "1"
- Android: versionname = "1.0" type = "codeph" text = "/codeph">
- <Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
- <Activity Android: Name = ". 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>
- <Uses-SDK Android: minsdkversion = "4"/>
- <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"/>
- </Manifest>
- 7) running result