Android Baidu map development (I) first experience: first experience of android

Source: Internet
Author: User

Android Baidu map development (I) first experience: first experience of android

Reprinted please indicate the source: http://blog.csdn.net/crazy1235/article/details/42614603 

When I made an app about location or location, I inevitably used the map function. Recently, due to the needs of the project, I have studied some functions of Baidu map, write a blog record. You are welcome to comment and correct it!

I. Apply for an AK (API Key)

To use the Baidu map sdk, you must apply for an api key for Baidu map. The application process is quite simple.

First register as a developer of Baidu, and then open the network "http://lbsyun.baidu.com/apiconsole/key" and submit the application:


The most important step to create an application is the security code ]. The security code consists of a [Digital Signature] And a [;] and a [package name. The package name is the package structure of the project you created. It refers to the package value under the manifest tag in AndroidManifest. xml.

Digital signature refers to the SHA1 value of the android signature certificate.

There are two methods to obtain a digital signature:

1. Method 1: Use eclipse to view details.

Open the preferences menu of eclipse, and you can see the value of SHA1 in [Build] Under Android, such:


2. Method 2: Use the keytool (built-in jdk) to view data.

In the console, enter [cd. android, and then enter keytool-list-v-keystore debug. keystore: Press enter. Then, you are prompted to enter the key library password. Enter android and press enter to display the value of SHA1.



After the digital signature is completed, it is OK to create the application. After the application is created, the corresponding AK, that is, the api key, is displayed in the Application List.

2. Download the SDK

Open http://developer.baidu.com/map/index.php? Title = androidsdk/sdkandev-download: download the sdk. You can download it all or customize it. In Versions later than V2.3.0, the SDK can be downloaded in a customizable manner. You can select the corresponding function to download the corresponding SDK according to your project needs.

3. Reference Baidu SDK in android Projects

1. Add the jar package and so files in the Development Kit to the libs file.



2. Add the development key and required permissions to AndroidManifest. xml.

<Application android: allowBackup = "true" android: icon = "@ drawable/ic_launcher" android: label = "@ string/app_name" android: theme = "@ style/AppTheme"> <meta-data android: name = "com. baidu. lbsapi. API_KEY "android: value =" fill in the AK you applied for "/>

Permission:

<! -- Baidu API required permissions --> <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 "/>
3. Add a map control to the layout file:

<com.baidu.mapapi.map.MapView        android:id="@+id/bmapview"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:clickable="true" />
4. initialize the Context global variable referenced by the SDK when the application is created.

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);//SDKInitializer.initialize(getApplicationContext());setContentView(R.layout.activity_main);init();}

Note that the initialize method must pass ApplicationContext, this, or MAinActivity. this does not work. Otherwise, an exception occurs during running. Therefore, Baidu recommends that you put this method in the initialization method of the Application.

Then, rewrite several methods of activity life cycle to manage the map life cycle. The onReusme, onPause, and onDestory methods of mapview are executed in the onResume, onPause, and onDestory methods of the activity.

Package com. bdmap. view; import com. baidu. mapapi. SDKInitializer; import com. baidu. mapapi. map. baiduMap; import com. baidu. mapapi. map. mapView; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. window; public class MainActivity extends Activity {// Baidu map control private MapView mMapView = null; // Baidu map object private BaiduMap bdMap; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); // SDKInitializer. initialize (getApplicationContext (); setContentView (R. layout. activity_main); init ();}/*** Initialization Method */private void init () {mMapView = (MapView) findViewById (R. id. bmapview) ;}@ Overrideprotected void onResume () {super. onResume (); mMapView. onResume () ;}@ Overrideprotected void onPause () {super. onPause (); mMapView. onPause () ;}@ Overrideprotected void onDestroy () {mMapView. onDestroy (); mMapView = null; super. onDestroy ();}}

After completing the preceding steps, you can complete a simple "Hello Map" program.

3. switching between common maps and satellite maps

Baidu maps are classified into two types: normal vector maps and satellite maps.

MMapView = (MapView) findViewById (R. id. bmapView); mBaiduMap = mMapView. getMap (); // common map mBaiduMap. setMapType (BaiduMap. MAP_TYPE_NORMAL); // satellite map mBaiduMap. setMapType (BaiduMap. MAP_TYPE_SATELLITE );
4. display real-time traffic diagram (Traffic diagram)

// Enable traffic map mBaiduMap. setTrafficEnabled (true );
5. Display heat map

A heat map is a graph that displays the page area and the geographical area of the visitor in a special highlighted form. In general, it is to show the density of people in a certain area on the map. Similar:

// Enable heat map mBaiduMap. setBaiduHeatMapEnabled (true );



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.