Android Baidu map API integrates basic map and Baidu map api

Source: Internet
Author: User

Android Baidu map API integrates basic map and Baidu map api

When I used the Baidu map API to integrate Baidu map, I encountered some pitfalls and opened this post...

Start Code directly

1. Create a project

We recommend that you use! If ES is used, a java. lang. noClassDefFoundError: com. baidu. location. the LocationClient error has been posted on Baidu for half a day. Most of the answers are the same and cannot solve the problem! (This is also a depressing thing for me. Why are there useless things? Some people are still turning around like crazy ...) Therefore, I finally chose to use.

2. Go to the Baidu map Homepage

1) if you do not have an account, apply for a Baidu account

2) download the sample code of Baidu map and decompress it (the download may fail, but there is no way to download it. Just a few more times ...)

Decompress the package as follows:

        

 

 

3. Integrate the basic map page with the Android map SDK according to the official documentation.

    

 

1) obtain the key

* Click to obtain the key.

       

 

 

* Enter information

       

    

Application name (in project value> String. xml> app_name)

SHA1 value: Command Line WINDOW> keytool-list-v-keystore FilePath (FilePath: Drive C> User>. android> debug. keystore)

          

          

 

Note: if an error 203 is returned, the verification code is incorrect.

Solution: 1. Copy the. APK file at the runtime, change the suffix to zip, and decompress it.

                    

2. Command Line WINDOW> keytool-printcert-file FilePath (FilePath: for example)

                    

3. To get the SHA1 value, it is best to re-create the application on the Baidu map open platform to obtain a new key (remember to modify it in AndroidManifest. xml)

 

Package name: AndroidManifest. xml

  

* Get a key.

       

2) Configuration

* Import the jar package and reference it

       

 

* Insert the following code in build. gradle> android:

 sourceSets {        main {            jniLibs.srcDirs = ['libs']        }    }

  

* Add a development Key to the application.

<Meta-data android: name = "com. baidu. lbsapi. API_KEY" android: value = "Developer key"/> <! -- This developer key is changed to the key obtained by the application created above -->

* Add required permissions

<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.WAKE_LOCK"/><uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><uses-permission android:name="android.permission.GET_TASKS" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.WRITE_SETTINGS" />

* Add a map control to the layout xml file.

1 <com.baidu.mapapi.map.MapView  2     android:id="@+id/bmapView"  3     android:layout_width="fill_parent"  4     android:layout_height="fill_parent"  5     android:clickable="true" />

 

* Initialize the Context global variable referenced by the SDK when creating an application to manage the map Lifecycle

Public class MainActivity extends Activity {MapView mMapView = null; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // initialize the context information before using the SDK components, and pass in the ApplicationContext // note that this method must be implemented before the setContentView method. initialize (getApplicationContext (); setContentView (R. layout. activity_main); // obtain the map control reference mMapView = (MapView) findViewById (R. id. bmapView) ;}@ Override protected void onDestroy () {super. onDestroy (); // execute mMapView when onDestroy is executed in activity. onDestroy () to implement mMapView for map lifecycle management. onDestroy () ;}@ Override protected void onResume () {super. onResume (); // execute mMapView when onResume is executed in the activity. onResume () to implement mMapView for map lifecycle management. onResume () ;}@ Override protected void onPause () {super. onPause (); // execute mMapView when onPause is executed in activity. onPause () enables mMapView for map lifecycle management. onPause ();}}

 

After completing the preceding steps, run the program to display the following map in your application:

      

 

 

 

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.