001 go to the android project of mobile development (Baidu map: Build a Baidu map development environment)

Source: Internet
Author: User

Today, I started to write my blog on Android. This time I will introduce the development of Baidu map for Android.

Before the official start, please note that when you repost a blog, please note the source

When developing Baidu map, the first step must be to build an android development environment. Because it is not just a beginner or a beginner developer who has not yet started, so I will not elaborate on the environment here.

To build an android environment, go to Baidu Library.

The following is a formal description of how to use Baidu map to develop applications on Android.

Step 1: Download Baidu official documentation --> click Baidu API documentation to download the full version of Android sdkv1.3.5 (raster map) used in this tutorial.

Step 2: Go to Baidu's official website to apply for the key that Baidu does not use for commercial purposes --> Step 2: apply for the key

Step 3: After all our keys and documents have been downloaded, you can start to write our code (create the following project)

Step 4: Add the following code to the resource definition file (androidmanifest. XML) of the project:

<! -- Add Baidu API authorization information --> <uses-Permission Android: Name = "android. permission. access_network_state "> </uses-Permission> <uses-Permission Android: Name =" android. permission. access_fine_location "> </uses-Permission> <uses-Permission Android: Name =" android. permission. internet "> </uses-Permission> <uses-Permission Android: Name =" android. permission. write_external_storage "> </uses-Permission> <uses-Permission Android: Name =" android. permission. access_wifi_state "> </uses-Permission> <uses-Permission Android: Name =" android. permission. change_wifi_state "> </uses-Permission> <uses-Permission Android: Name =" android. permission. read_phone_state "> </uses-Permission> <uses-Permission Android: Name =" android. permission. call_phone "> </uses-Permission>

Step 5: Add Baidu map to support the screen. It is also in the resource definition file.

<! -- Add screen support for Baidu map --> <supports-screens Android: largescreens = "true" Android: normalscreens = "false" Android: smallscreens = "true" Android: resizeable = "true" Android: anydensity = "true"/>

Resource: All content of the resource definition file is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. shuaiyin. baidu "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <uses-SDK Android: minsdkversion =" 10 "/> <application Android: icon = "@ drawable/ic_launcher" Android: Label = "@ string/app_name"> <activity Android: Label = "@ string/app_name" Android: Name = ". baidu_suyiactivity "> <intent-filter> <action Android: Name =" android. Intent. action. main "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter> </activity> </Application> <! -- Add Baidu API authorization information --> <uses-Permission Android: Name = "android. permission. access_network_state "> </uses-Permission> <uses-Permission Android: Name =" android. permission. access_fine_location "> </uses-Permission> <uses-Permission Android: Name =" android. permission. internet "> </uses-Permission> <uses-Permission Android: Name =" android. permission. write_external_storage "> </uses-Permission> <uses-Permission Android: Name =" Ndroid. permission. access_wifi_state "> </uses-Permission> <uses-Permission Android: Name =" android. permission. change_wifi_state "> </uses-Permission> <uses-Permission Android: Name =" android. permission. read_phone_state "> </uses-Permission> <uses-Permission Android: Name =" android. permission. call_phone "> </uses-Permission> <! -- Add screen support for Baidu map --> <supports-screens Android: largescreens = "true" Android: normalscreens = "false" Android: smallscreens = "true" Android: resizeable = "true" Android: anydensity = "true"/> </manifest>

Part 6: import all jar packages of Baidu map to the Project

Copy the file shown in "baidumapapi_sample_android_1.3.5" in the example document "baidumapapi_sample_android_1.3.5" officially provided by Baidu. Right-click build path --> Add to build path on the jar package.

Step 7: Define the map of the Baidu map control in Main. xml

<! -- Add controls for displaying Baidu maps --> <COM. baidu. mapapi. mapview Android: Id = "@ + ID/bmapview" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: clickable = "true"/>

Resource: All code in Main. xml

<? 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: Orientation = "vertical"> <! -- Add controls for displaying Baidu maps --> <COM. baidu. mapapi. mapview Android: Id = "@ + ID/bmapview" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: clickable = "true"/> </linearlayout>

Step 8: The environment is basically set up. The following code is written mainly in the middle of baidu_suyiactivity.java.

1. Let baidu_suyiactivity inherit the mapactivity (COM. Baidu. mapapi. mapactivity) provided by Baidu map)

2. Add the definition of Baidu map-related controls

// Add the control private mapview related to Baidu map; // load the private bmapmanager caused by Baidu map; // define Baidu map's keyprivate string key = "* I have processed the key * 729dd94b0429a4bee30797e04d91b0211c4"; // Add the corresponding control private mapcontroller on Baidu map;

3. Implement the following method when inheriting mapactivity.

@Overrideprotected boolean isRouteDisplayed() {return false;}

4. Write the following code in the oncreate method:

// First instantiate mapview = (mapview) This. findviewbyid (R. id. bmapview); bmapmanager = new bmapmanager (baidu_suyiactivity.this); // call Baidu map to load key bmapmanager. init (Key, new mkgenerallistener () {@ overridepublic void ongetpermissionstate (INT arg0) {If (arg0 = 300) {toast. maketext (baidu_suyiactivity.this, "the key you entered is incorrect. Please verify", 2 ). show () ;}@ overridepublic void ongetnetworkstate (INT arg0) {}}); this. initmapactivity (bmapmanager); // indicates that the zoom function mapview can be set. setbuiltinzoomcontrols (true); mapcontroller = mapview. getcontroller (); // mark a central point geopoint = new geopoint (INT) (39.915*1e6), (INT) (116.404*1e6) on Baidu map )); // set a central point mapcontroller for the map object. setcenter (geopoint); // sets the map's Zoom level mapcontroller. setzoom (12 );

5. Final Implementation

Ondestroy () --> onresume () --> onpause () method

@Override    protected void onDestroy() {    super.onDestroy();    if(bMapManager != null){    bMapManager.destroy();    bMapManager = null;    }    }    @Override    protected void onResume() {    super.onResume();    if(bMapManager != null){    bMapManager.start();    }    }    @Override    protected void onPause() {    super.onPause();    if(bMapManager != null){    bMapManager.stop();    }    }

 

Resource: baidu_suyiactivity all code

Package COM. shuaiyin. baidu; import android. OS. bundle; import android. widget. toast; import COM. baidu. mapapi. bmapmanager; import COM. baidu. mapapi. geopoint; import COM. baidu. mapapi. mkgenerallistener; import COM. baidu. mapapi. mapactivity; import COM. baidu. mapapi. mapcontroller; import COM. baidu. mapapi. mapview;/*** allow Baidu map to inherit mapactivity * @ author shuaiyin **/public class baidu_suyiactivity extends mapactivity {// Add Add the relevant control of Baidu map private mapview; // load the private bmapmanager caused by Baidu map; // define Baidu map's keyprivate string key = "* I have processed the key * 729dd94b0429a4bee30797e04d91b0211c4"; // Add the corresponding control private mapcontroller on Baidu map; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // first instantiate mapview = (mapview) This. findviewbyid (R. id. bmap View); bmapmanager = new bmapmanager (baidu_suyiactivity.this); // call Baidu map to load key bmapmanager. init (Key, new mkgenerallistener () {@ overridepublic void ongetpermissionstate (INT arg0) {If (arg0 = 300) {toast. maketext (baidu_suyiactivity.this, "the key you entered is incorrect. Please verify", 2 ). show () ;}@ overridepublic void ongetnetworkstate (INT arg0) {}}); this. initmapactivity (bmapmanager); // indicates that the zoom function mapview can be set. setbuiltinzoomcontrols (tru E); mapcontroller = mapview. getcontroller (); // mark a central point geopoint = new geopoint (INT) (39.915*1e6), (INT) (116.404*1e6) on Baidu map )); // set a central point mapcontroller for the map object. setcenter (geopoint); // sets the map's Zoom level mapcontroller. setzoom (12) ;}@ override protected void ondestroy () {super. ondestroy (); If (bmapmanager! = NULL) {bmapmanager. Destroy (); bmapmanager = NULL ;}@override protected void onresume () {super. onresume (); If (bmapmanager! = NULL) {bmapmanager. Start () ;}@ override protected void onpause () {super. onpause (); If (bmapmanager! = NULL) {bmapmanager. Stop () ;}@ overrideprotected Boolean isroutedisplayed () {return false ;}}

Result of running in Simulator

Finally, this is done. Is it very easy to use Baidu map to develop an app! As long as you are willing to work hard, nothing can be done. Move to mobile development with me.

If you do not know anything, you can leave a message or send a private message to me.

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.