Steps for using Baidu map on Android

Source: Internet
Author: User
Tags imap

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

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: Orientation = "vertical"
  4. Android: layout_width = "fill_parent"
  5. Android: layout_height = "fill_parent"
  6. >
  7. <Com. Baidu. mapapi. mapview Android: Id = "@ + ID/map_view"
  8. Android: layout_width = "fill_parent"
  9. Android: layout_height = "fill_parent"
  10. Android: clickable = "true"
  11. />
  12. </Linearlayout>


5) Create an activity to inherit from Com. Baidu. mapapi. mapactivity

[Java]View plaincopy

  1. Package com. Liufeng. baidumap;
  2. Import Android. Graphics. drawable. drawable;
  3. Import Android. OS. Bundle;
  4. Import com. Baidu. mapapi. bmapmanager;
  5. Import com. Baidu. mapapi. geopoint;
  6. Import com. Baidu. mapapi. mapactivity;
  7. Import com. Baidu. mapapi. mapcontroller;
  8. Import com. Baidu. mapapi. mapview;
  9. Public class mainactivity extends mapactivity {
  10. Private bmapmanager mapmanager;
  11. Private mapview;
  12. Private mapcontroller;
  13. @ Override
  14. Public void oncreate (bundle savedinstancestate ){
  15. Super. oncreate (savedinstancestate );
  16. Setcontentview (R. layout. Main );
  17. // Initialize mapactivity
  18. Mapmanager = new bmapmanager (getapplication ());
  19. // The first parameter of the init method must be filled with the applied API key.
  20. Mapmanager. INIT ("285b450ebab2a92293e85502150ada7f03c777c4", null );
  21. Super. initmapactivity (mapmanager );
  22. Mapview = (mapview) findviewbyid (R. Id. map_view );
  23. // Set map mode to traffic map
  24. Mapview. settraffic (true );
  25. // Set to enable the built-in zoom control
  26. Mapview. setbuiltinzoomcontrols (true );
  27. // Construct a geopoint (latitude and longitude) with the given latitude and longitude)
  28. Geopoint point = new geopoint (INT) (47.118440*1e6), (INT) (87.493147*1e6 ));
  29. // Create a tag maker
  30. Drawable marker = This. getresources (). getdrawable (R. drawable. iconmarka );
  31. // Define location and boundary for maker
  32. Marker. setbounds (0, 0, marker. getintrinsicwidth (), marker. getintrinsicheight ());
  33. // Obtain the map controller object for controlling mapview
  34. Mapcontroller = mapview. getcontroller ();
  35. // Set the map center
  36. Mapcontroller. setcenter (point );
  37. // Set the default zoom level of the map
  38. Mapcontroller. setzoom (12 );
  39. }
  40. @ Override
  41. Protected Boolean isroutedisplayed (){
  42. Return false;
  43. }
  44. @ Override
  45. Protected void ondestroy (){
  46. If (mapmanager! = NULL ){
  47. Mapmanager. Destroy ();
  48. Mapmanager = NULL;
  49. }
  50. Super. ondestroy ();
  51. }
  52. @ Override
  53. Protected void onpause (){
  54. If (mapmanager! = NULL ){
  55. Mapmanager. Stop ();
  56. }
  57. Super. onpause ();
  58. }
  59. @ Override
  60. Protected void onresume (){
  61. If (mapmanager! = NULL ){
  62. Mapmanager. Start ();
  63. }
  64. Super. onresume ();
  65. }
  66. }
  67. 6) Configure in androidmanifest. xml
    [XHTML]View plaincopy

    1. <? XML version = "1.0" encoding = "UTF-8"?>
    2. <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
    3. Package = "com. Liufeng. baidumap"
    4. Android: versioncode = "1"
    5. Android: versionname = "1.0" type = "codeph" text = "/codeph">
    6. <Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
    7. <Activity Android: Name = ". mainactivity" Android: Label = "@ string/app_name">
    8. <Intent-filter>
    9. <Action Android: Name = "android. Intent. Action. Main"/>
    10. <Category Android: Name = "android. Intent. Category. launcher"/>
    11. </Intent-filter>
    12. </Activity>
    13. </Application>
    14. <Uses-SDK Android: minsdkversion = "4"/>
    15. <Uses-Permission Android: Name = "android. Permission. access_network_state"/>
    16. <Uses-Permission Android: Name = "android. Permission. access_fine_location"/>
    17. <Uses-Permission Android: Name = "android. Permission. Internet"/>
    18. <Uses-Permission Android: Name = "android. Permission. write_external_storage"/>
    19. <Uses-Permission Android: Name = "android. Permission. access_wifi_state"/>
    20. <Uses-Permission Android: Name = "android. Permission. change_wifi_state"/>
    21. <Uses-Permission Android: Name = "android. Permission. read_phone_state"/>
    22. </Manifest>
    23. 7) running result


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.