Baidu Map Android Sdk-hello Baidu Map

Source: Internet
Author: User

The following content mainly to the general developers how to use the Baidu Map Android SDK to build a most basic map application!

The first step is to create an Android project and import the development package of the Baidu Maps Android SDK into the project location:

Create a new Libs folder in the project, copy the Baidumapapi_vx_x_x.jar from the development package to the Libs root directory and copy libbaidumapsdk_vx_x_x.so to Libs\ Armeabi Directory (the official website has already had these two files, if you want to integrate into your project, you need to add it), copy the completed project catalog as shown;

Note: liblocsdk3.so and Locsdk_3.1.jar are the resources used by Baidu to locate the SDK, and developers can add their own according to their needs.


The second step is to add the appropriate jar (add jar) to the built Android project:

In the project Properties->java Build path->libraries, select "Add External JARs", and the Baidumapapi_vx_x_x.jar is selected and returned when determined.

With the above two steps, you will be able to use the Baidu Map SDK to provide you with all the functions.

Note: Because of the ADT plugin upgrade, if you are using Eclipse ADT 22, you need to set the development environment accordingly, as follows:

1. Select project in Eclipse, right-click Properties->java Build path->order and Export to make Android Private libraries tick;

2. Project-clean-> clean all.

The third step is to add the development key in the project's Androidmanifest file:

The core code is as follows, please refer to the blog: Baidu map Android SDK-Request Development key

<application>      <meta-data          android:name= "Com.baidu.lbsapi.API_KEY"        android:value= "developer KEY"/ >  </application>

Fourth step, add the required permissions in the project's Androidmanifest file:

The required permissions are as follows, added in the Androidmanifest file:

<uses-permission android:name= "Android.permission.GET_ACCOUNTS"/> <uses-permission android:name= " Android.permission.USE_CREDENTIALS "/> <uses-permission android:name=" Android.permission.MANAGE_ACCOUNTS "/ > <uses-permission android:name= "Android.permission.AUTHENTICATE_ACCOUNTS"/> <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.CHANGE_WIFI_STATE "/> <uses-permission Android:name= "Android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name= " Android.permission.READ_PHONE_STATE "/> <uses-permission android:name=" android.permission.WRITE_EXTERNAL_ STORAGE "/> <uses-permission android:name=" Android.permission.BROADCAST_STICKY "/> <uses-permission Android:name= "Android.permissioN.write_settings "/> <uses-permission android:name=" Android.permission.READ_PHONE_STATE "/> 

Fifth step, add the map control (Mapview) to the XML layout file:

Add the Mapview control as follows:

<com.baidu.mapapi.map.mapview      android:id= "@+id/bmapview"      android:layout_width= "Fill_parent      " android:layout_height= "Fill_parent"      android:clickable= "true"/>

The sixth step in app creation is to initialize the SDK:

Note: You need to call Sdkinitializer.initialize (Getapplicationcontext ()) before each functional component of the SDK is used, so we recommend that the method be placed in the application initialization method.

public class Mainactivity extends Activity {      @Override      protected void onCreate (Bundle savedinstancestate) {          super.oncreate (savedinstancestate);           Initialize the context information before using each component of the SDK, passing in ApplicationContext          //Note that the method is implemented before Setcontentview the method          sdkinitializer.initialize (Getapplicationcontext ());          Setcontentview (R.layout.activity_main);      }  }

The seventh step is to create a map activity file and manage its life cycle:

public class Mainactivity extends Activity {Mapview mmapview = null;           @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Initialize the context information before using each component of the SDK, passing in ApplicationContext//Note that the method is implemented before Setcontentview the method sdkinitializer.initialize          (Getapplicationcontext ());          Setcontentview (R.layout.activity_main);      Gets the map control reference Mmapview = (Mapview) Findviewbyid (R.id.bmapview);          } @Override protected void OnDestroy () {Super.ondestroy ();      Execute Mmapview.ondestroy () when activity executes OnDestroy, implement map life cycle management Mmapview.ondestroy ();          } @Override protected void Onresume () {super.onresume (); Executes Mmapview when the activity executes Onresume.          Onresume () To achieve Map Lifecycle management mmapview.onresume ();          } @Override protected void OnPause () {super.onpause (); Executes Mmapview when the activity executes OnPause. OnPause () for map life cycle management MMAPVIEw.onpause (); }      }

Eighth step, run the app and show the base map:


Baidu Map Android Sdk-hello Baidu Map

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.