Android cainiao Study Notes 30 ---- use Baidu map API for Android (I) preparations and display maps in applications, and android Baidu map api

Source: Internet
Author: User

Android cainiao Study Notes 30 ---- use Baidu map API for Android (I) preparations and display maps in applications, and android Baidu map api

1. Preparations:

Baidu map API is open for free, but you need to apply for API Key:

1) register a Baidu developer account first

2) Enter Baidu open service platform http://developer.baidu.com/

 

3) enter the LBS cloud

 

4) Click the API console in the upper-right corner. If you are not logged on, the logon page is displayed. After Successful Logon, the application console is displayed:

 

5) Click Create application:

 

Select Android SDK as the application type. Pay attention to the following security code. The format is digital signature + application package name, separated by semicolons. Digital signature acquisition:

In Eclipse, window-> Preferences-> Android-> build: Find the column SHA1 fingerprint:

 

Copy a long string and use the digital signature for the test.

Then there is the app package name. Just give the app that will use the Baidu map API a package name. My app is cn. csc. bm. The two are concatenated with the security code:

B0: B1: CA: 82: 82: AC: 84: 0A: 73: 56: 56: 12: C6: C8: BB: 08: 3C: 93: FE: 04; cn. csc. bm, and then click submit:

 

This is the API Key corresponding to the above security code.

After obtaining the API Key, you need to download the Baidu map SDK:

 


The upgrade of the Baidu map SDK to v3.5.0 is somewhat different from the previous 2.x, making it easier.

Download it to the relevant download:

 

Select the download content as needed. Here I click one-click Download.

In the downloaded package:

 

Docs is a help document

Lib is the class library to be introduced when using Baidu map API

Sample is an example of Some APIs.

Decompress the Lib package

 

Copy the jar package to the libs directory of the project and right-click the jar package:

 

Add it to build path

Go to the so directory and copy the linked libraries of different CPU versions together with the directory to the libs directory of the project.

 

For example, armeabi copies the directory together with the so Link Library to the libs directory and runs it on the arm Simulator. Note that: if the CPU used by the simulator is x86, an error occurs:

 

Because the so Link Library is written in c/c ++, and then compiled into a specific version based on a specific CPU platform, the machine commands of different CPUs are different and certainly cannot be used in general. In this case, copy the x86 directory to the libs directory.

 

At this time, both arm and x86 simulators can use Baidu API normally:

 

5554 uses the arm CPU, and 5556 uses the X86cpu, which can run normally.

 

2. Use Baidu map API in applications:

The usage of the new version of Baidu map SDK is different from that of BMapManager. The specific operations are as follows:

1) Add the development Key to the application node in the configuration file, that is, the API Key applied for above:

1 <application> 
2 
3     <meta-data 
4 
5         android:name="com.baidu.lbsapi.API_KEY" 
6 
7         android:value="API key" /> 
8 
9 </application>

2) Add the permissions required by Baidu map SDK to the list file:

 1 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 2 
 3 <uses-permission android:name="android.permission.INTERNET"/>
 4 
 5 <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
 6 
 7 <uses-permission android:name="android.permission.WAKE_LOCK"/>
 8 
 9 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
10 
11 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
12 
13 <uses-permission android:name="android.permission.GET_TASKS" />
14 
15 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
16 
17 <uses-permission android:name="android.permission.WRITE_SETTINGS" />

3) Add a map display control to the layout file:

1 <com.baidu.mapapi.map.MapView 
2 
3     android:id="@+id/mv" 
4 
5     android:layout_width="match_parent" 
6 
7     android:layout_height="match_parent" 
8 
9 android:clickable="true" />

4) initialize the Context global variable referenced by the SDK when creating an application:

 1 public class MainActivity extends Activity {
 2 
 3     @Override
 4 
 5     protected void onCreate(Bundle savedInstanceState) {
 6 
 7         super.onCreate(savedInstanceState);
 8 
 9         SDKInitializer.initialize(getApplicationContext());
10 
11         setContentView(R.layout.activity_main);
12 
13     }
14 
15 }


Note that the initialization must be completed before setContentView (), because the MapView () in the layout file is automatically obtained during the initialization, and the map can be displayed normally only after the layout file is loaded.

In this way, Baidu map can be displayed in the application. However, for resource and other reasons, you also need to bind the life cycle of the map control with the Activity life cycle:

5) modify the MainActivity. java code:

 1 public class MainActivity extends Activity {
 2 
 3       private MapView mv;
 4 
 5     @Override
 6 
 7     protected void onCreate(Bundle savedInstanceState) {
 8 
 9         super.onCreate(savedInstanceState);
10 
11         SDKInitializer.initialize(getApplicationContext());
12 
13         setContentView(R.layout.activity_main);
14 
15         mv = (MapView) findViewById(R.id.mv);
16 
17     }
18 
19       @Override
20 
21       protected void onResume() {
22 
23            // TODO Auto-generated method stub
24 
25            super.onResume();
26 
27            mv.onResume();
28 
29       }
30 
31       @Override
32 
33       protected void onPause() {
34 
35            // TODO Auto-generated method stub
36 
37            super.onPause();
38 
39            mv.onPause();
40 
41       }
42 
43       @Override
44 
45       protected void onDestroy() {
46 
47            // TODO Auto-generated method stub
48 
49            super.onDestroy();
50 
51            mv.onDestroy();
52 
53       }
54 
55 }

Call the corresponding method of MapView In the lifecycle callback method corresponding to the Activity.

6) running result:

 

 

The above simple program only uses two classes:

 

Used to initialize the SDK context global variable and specify the sdcard path

Common Methods:

 

It is used to initialize the context environment and must be called before various components of Baidu map SDK, such as MapView, are used.

 

MapView inherits from ViewGroup.

Methods:

 

Because it inherits from ViewGroup, there are addView () and removeView () methods, which can be used to add and delete subcontrols.

OnDestroy (), onPause (), and onResume () correspond to the callback of the lifecycle respectively.

ShowScaleControl (boolean) and showZoomControls (boolean) are used to control whether the scale control and zoom control are displayed.

SetScaleControlPosition () and setZoomControlsPosition () are used to set the display positions of the two controls.

GetMap () is used to obtain the map controller object.

The following describes the effects of several methods:

1) addView ():

Create a layout file for ction. xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 
 3 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 4 
 5     android:layout_width="match_parent"
 6 
 7     android:layout_height="match_parent" >
 8 
 9     <Button
10 
11         android:id="@+id/btn_cent"
12 
13         android:layout_width="wrap_content"
14 
15         android:layout_height="wrap_content"
16 
17         android:text="中"
18 
19         android:layout_centerInParent="true"
20 
21         />
22 
23     <Button
24 
25         android:id="@+id/btn_up"
26 
27         android:layout_width="wrap_content"
28 
29         android:layout_height="wrap_content"
30 
31         android:text="上"
32 
33         android:layout_above="@id/btn_cent"
34 
35         android:layout_alignLeft="@id/btn_cent"
36 
37         />
38 
39     <Button
40 
41         android:id="@+id/btn_down"
42 
43         android:layout_width="wrap_content"
44 
45         android:layout_height="wrap_content"
46 
47         android:text="下"
48 
49         android:layout_below="@id/btn_cent"
50 
51         android:layout_alignLeft="@id/btn_cent"
52 
53         />
54 
55     <Button
56 
57         android:id="@+id/btn_left"
58 
59         android:layout_width="wrap_content"
60 
61         android:layout_height="wrap_content"
62 
63         android:text="左"
64 
65         android:layout_toLeftOf="@id/btn_cent"
66 
67         android:layout_alignTop="@id/btn_cent"
68 
69         />
70 
71     <Button
72 
73         android:id="@+id/btn_down"
74 
75         android:layout_width="wrap_content"
76 
77         android:layout_height="wrap_content"
78 
79         android:text="上"
80 
81         android:layout_toRightOf="@id/btn_cent"
82 
83         android:layout_alignTop="@id/btn_cent"
84 
85         />
86 
87 </RelativeLayout>

Displays the top, bottom, and left buttons.

Modify MainActivity. java:

 1 protected void onCreate(Bundle savedInstanceState) {
 2 
 3         super.onCreate(savedInstanceState);
 4 
 5         SDKInitializer.initialize(getApplicationContext());
 6 
 7         setContentView(R.layout.activity_main);
 8 
 9         mv = (MapView) findViewById(R.id.mv);
10 
11         View view = getLayoutInflater().from(this).inflate(R.layout.direction, null);
12 
13         Builder builder = new MapViewLayoutParams.Builder();
14 
15         MapViewLayoutParams params = builder.point(new Point(100,450)).width(300).height(300).build();
16 
17         mv.addView(view,params);
18 
19 }

Note: The second parameter of the addView () method must be of the MapViewLayoutParams type. For details, you can use the internal class Builder to set various attributes and create an instance.

For example, in the code above, the position, width, and height of the sub-control are set through the MapViewLayoutParams. Builder object. Finally, the build () method is called to obtain the MapViewLayoutParams object.

Running result:

 

As you can see, the custom control is placed on the map, although it does not work.

2) showZoomControls ():

This control is displayed by default. You can call this method to input false if you do not need it.

 

3) showScaleControl ():

It is displayed by default, that is, the above 5 km stuff. If it is not displayed, this method is called and false is passed in.

 

4) modify the display position of the control:

SetZoomControlsPosition ();

SetScaleControlPosition ();

However, direct call:

Mv. setScaleControlPosition (new Point (20, 20 ));

Mv. setZoomControlsPosition (new Point (150,50 ));

The location has not changed, as described in the document:

 

Where is onMapLoaFinish ......

MapView does not have this method, so you have to put it on hold first and test the next method:

5) getMap ():

Used to obtain the map controller.

BaiduMap map = mv. getMap (); // The return type is BaiduMap, which is different from the Controller of previous versions.

There are many methods for the BaiduMap class. It seems that you have to learn it slowly. However, the help document noticed that such a method can be used now:

 

Set the callback for map loading. The method for setting the location above is to take effect after the map is loaded. Do you need to set it in this callback? A simple test is performed:

1 BaiduMap map = mv.getMap();
 2 
 3 map.setOnMapLoadedCallback(new OnMapLoadedCallback() {
 4 
 5                 
 6 
 7                  @Override
 8 
 9                  public void onMapLoaded() {
10 
11                       // TODO Auto-generated method stub
12 
13                       mv.setScaleControlPosition(new Point(20,20));
14 
15                    mv.setZoomControlsPosition(new Point(150,50));
16 
17                  }
18 
19 });

Running result:

 

It takes effect ...... Khan, help document dare not blur a bit, but also need to explore ......

Learn this ......

After all, it's a weekend. I'll watch a movie in the afternoon, and other APIs will come back later to study ......


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.