Official use of Baidu maps

Source: Internet
Author: User

Baidu Map SDK for developers to provide a convenient display of Baidu map data interface, through the following steps, you can use the Baidu map data in your application:

The first step: Create and configure the project (see the Project configuration section for details);

The second step: Add the development key, the required permission and other information in the androidmanifest;

(1) Add a development key in application

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

2) Add the 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 "/>

The third step is to add a map control to the layout XML file;

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

The fourth step is to initialize the context global variables referenced by the SDK when the application is created:

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 Sdkinitializer.initialize before the method is Setcontentview (g          Etapplicationcontext ());      Setcontentview (R.layout.activity_main); }  }

Note: before each functional component of the SDK is used, you need to call

sdkinitializer.initialize (Getapplicationcontext ()), so we recommend that the method be placed in the application initialization method

Fifth step, create map activity, manage map life cycle;

public class mainactivity extends activity {       mapview mmapview = null;       @Override        protected void oncreate (bundle savedinstancestate)  {           super.oncreate (savedinstancestate);            //initializes the context information before using each component of the SDK, passing in the applicationcontext           //Note that this method will be implemented before Setcontentview method            sdkinitializer.initialize (Getapplicationcontext ());           setcontentview (R.layout.activity_main);           //Get map control references           mMapView =  ( Mapview)  findviewbyid (R.id.bmapview);       }       @Override        protected void ondestroy ()  {           Super.ondestroy ();           // Perform Mmapview.ondestroy () when activity executes OnDestroy to achieve map lifecycle management            mmapview.ondestroy ();      }       @Override       protected void onresume ()  {           super.onresume ();           // Perform mmapview. onresume  () when activity executes onresume to achieve map lifecycle management            mmapview.onresume ();          }        @Override       protected void onpause ()  {           super.onpause ();           // Perform mmapview. onpause  () when activity executes onPause to achieve map lifecycle management            mmapview.onpause ();          }       }

After completing the above steps, run the program to display the following map in your app:

The above example shows you how to build a basic map page. The map control supports multiple instances since the v2.3.5 version, where developers can create multiple map objects in a single page and operate on them independently without interfering with each other. Please refer to the relevant instructions in Mutimapviewdemo for specific use.

In addition, starting from the v2.3.5 version. The Mapview control also adds support for the fragment framework. The user can use the Supportmapfragment control to complete the development work within the appropriate framework (see MAPFRAGMENTDEMO).

Official use of Baidu maps

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.