Use of Baidu maps for Android development

Source: Internet
Author: User

Haven't written a blog for a long time, recently encountered a new need to use Baidu Map of the basic map, positioning, in theory should also use the Eagle Eye function bar. It's hard to say.

Just start at the beginning and record it from scratch.

First of all, the first application of a Baidu Map API Key Process Official website is very detailed I will not say more. (Linux system under Android Studio get SHA1: My blog http://www.cnblogs.com/wobeinianqing/p/5594694.html, the official website is also introduced)

Download the relevant package after the key has been applied (there is a relevant download under Development tab).

Select the desired function and you can download it later:

After the completion of the extraction is generally a jar file with a number of different CPU schemas within the folder is a bunch of. os files

Create an Android project in the file (progect view) app/libs file to enter the jar file and then select the Jar package right-click on the Add as library ...

Then create the Jnilibs folder in the app/src/main/directory to copy into the folder above

After completion, the directory structure is as follows:

Below you can start to formally enter the programming mode!

can refer to Baidu Map API Hello Baidumap part

First is activity_main.xml (introduced Mapview)

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"App:layout_behavior= "@string/appbar_scrolling_view_behavior"Tools:context= "Com.zfls.findme.MainActivity"Tools:showin= "@layout/app_bar_main">    <Com.baidu.mapapi.map.MapViewAndroid:id= "@+id/bmapview"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:clickable= "true" /></Relativelayout>

Mainactivity.java

 Public classMainactivityextendsActivity {mapview Mmapview=NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //initializes the context information before using each component of the SDK, passing in the ApplicationContext//Note that this method should be implemented before setcontentview the methodsdkinitializer.initialize (Getapplicationcontext ());          Setcontentview (R.layout.activity_main); //Get map Control ReferenceMmapview =(Mapview) Findviewbyid (R.id.bmapview); } @Overrideprotected voidOnDestroy () {Super. OnDestroy ();      Mmapview.ondestroy (); } @Overrideprotected voidOnresume () {Super. Onresume ();          Mmapview.onresume (); } @Overrideprotected voidOnPause () {Super. OnPause ();          Mmapview.onpause (); }      }

This will enable the display of the map function.

Add the necessary permissions for the app below

<uses-permissionAndroid:name= "Android.permission.ACCESS_NETWORK_STATE"/><uses-permissionAndroid:name= "Android.permission.INTERNET"/><uses-permissionAndroid:name= "Com.android.launcher.permission.READ_SETTINGS" /><uses-permissionAndroid:name= "Android.permission.WAKE_LOCK"/><uses-permissionAndroid:name= "Android.permission.CHANGE_WIFI_STATE" /><uses-permissionAndroid:name= "Android.permission.ACCESS_WIFI_STATE" /><uses-permissionAndroid:name= "Android.permission.GET_TASKS" /><uses-permissionAndroid:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permissionAndroid:name= "Android.permission.WRITE_SETTINGS" />

Add your own key

< Meta-data           Android:name = "Com.baidu.lbsapi.API_KEY"           android:value= "Your key"/>  

Overall effect:

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.zfls.findme">    <uses-permissionAndroid:name= "Com.android.launcher.permission.READ_SETTINGS" />    <!--This permission is used for network positioning -    <uses-permissionAndroid:name= "Android.permission.ACCESS_COARSE_LOCATION" />    <!--This permission is used to access GPS location -    <uses-permissionAndroid:name= "Android.permission.ACCESS_FINE_LOCATION" />    <!--for accessing WiFi network information, WiFi information will be used for network location -    <uses-permissionAndroid:name= "Android.permission.ACCESS_WIFI_STATE" />    <!--get carrier information to support the interface that provides carrier-related information -    <uses-permissionAndroid:name= "Android.permission.ACCESS_NETWORK_STATE" />    <!--used to read the current state of the phone -    <uses-permissionAndroid:name= "Android.permission.READ_PHONE_STATE" />    <!--Write extended storage, write data to the expansion card for writing offline location data -    <uses-permissionAndroid:name= "Android.permission.WRITE_EXTERNAL_STORAGE" />    <!--access network, network location requires Internet access -    <uses-permissionAndroid:name= "Android.permission.INTERNET" />    <ApplicationAndroid:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:supportsrtl= "true"Android:theme= "@style/apptheme">        <Meta-dataAndroid:name= "Com.baidu.lbsapi.API_KEY"Android:value= "Your Key" />        <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name"Android:theme= "@style/apptheme.noactionbar">            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>            </Application></Manifest>

Below, plug in your phone run and experience your own map!

Why is Beijing in the middle? We'll replace it in the next section!

Use of Baidu maps developed by Android

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.