Android Studio configuration using Baidu API (with simple example)

Source: Internet
Author: User

Or with the students to develop the course work of the program app, to use the Baidu map API

However, the official documentation seems to be only an example of eclipse, which does not seem to explain to Android studio. Is it because the latter is the product of "Doodle"? Oh

The use of the API is simple. Class field and method, the official documentation has been given in detail.

And before your app uses the API, you need to apply a key to your app on the Baidu map developer's website, or the server won't talk to you. These official documents are all there, so don't dwell on them.

Http://developer.baidu.com/map/index.php?title=android-locsdk/geosdk-android-download

In the SDK above, just pick a version to download, I'm under the latest version 5.1

To get a folder Baidulbs_androidsdk_lib, the directory structure is as follows:

This leaves two questions:

1. How to configure the jar files and so files in the downloaded SDK into Android Studio

2. How to configure Manifest

Workaround:

1. Place the jar file in the Libs directory of Project structure:

Right-click the jar file and add it to the library (add as libraries)

Then, create a subdirectory under Libs: Armeabi, put the so file into this subdirectory

2. Androidmanifest.xml configuration, the official documentation of the demo inside. But if you don't bother, look down and change a little.

In manifest, add a child element:

<uses-permissionAndroid:name= "Android.permission.BAIDU_LOCATION_SERVICE"></uses-permission>    <uses-permissionAndroid:name= "Android.permission.ACCESS_COARSE_LOCATION"></uses-permission>    <uses-permissionAndroid:name= "Android.permission.ACCESS_FINE_LOCATION"></uses-permission>    <uses-permissionAndroid:name= "Android.permission.ACCESS_WIFI_STATE"></uses-permission>    <uses-permissionAndroid:name= "Android.permission.ACCESS_NETWORK_STATE"></uses-permission>    <uses-permissionAndroid:name= "Android.permission.CHANGE_WIFI_STATE"></uses-permission>    <uses-permissionAndroid:name= "Android.permission.READ_PHONE_STATE"></uses-permission>    <uses-permissionAndroid:name= "Android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>    <uses-permissionAndroid:name= "Android.permission.INTERNET"></uses-permission>    <uses-permissionAndroid:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>    <uses-permissionAndroid:name= "Android.permission.READ_LOGS"></uses-permission>

In application, add a child element:

<ServiceAndroid:name= "COM.BAIDU.LOCATION.F"android:enabled= "true"android:process= ": Remote" >        <Intent-filter>            <ActionAndroid:name= "com.baidu.location.service_v2.2" >                </Action>        </Intent-filter></Service><!--Meta-data needs to be written in application. -    <Meta-dataAndroid:name= "Com.baidu.lbsapi.API_KEY"Android:value= "Please enter the key for the application" />

Is it a little strange to feel the config file? But it worked.

Now you're ready to write code using the API normally.

Here, give me a simple example of its function is to get the current latitude and Longitude button once per click.

Xml:

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context=". MyActivity "    >    <TextViewAndroid:id= "@+id/show_position"Android:text= "@string/hello_world"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" />    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal"android:gravity= "Center_horizontal"        >        <ButtonAndroid:id= "@+id/control_button"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Get pos per click"Android:layout_marginleft= "100DP"            />    </LinearLayout></LinearLayout>

Java:

 PackageCom.example.user.testmap;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;Importcom.baidu.location.BDLocation;ImportCom.baidu.location.BDLocationListener;Importcom.baidu.location.LocationClient;Importcom.baidu.location.LocationClientOption; Public classMyActivityextendsActivity { PublicTextView show_position;  PublicButton getpositiononce; Privatelocationclient mlocationclient; Privatelocationclientoption moption;  Public DoubleLatitude, longitude; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.activity_my); Show_position=(TextView) Findviewbyid (r.id.show_position); Getpositiononce=(Button) Findviewbyid (R.id.control_button); Mlocationclient=NewLocationclient ( This); Moption=Newlocationclientoption (); /*Setting Options*/Moption.setopengps (true); Moption.setcoortype ("Bd09ll"); Moption.setscanspan (100);//every 0.1s, scan once (should be the meaning of satellite positioning)        /*set option option on local access client side*/mlocationclient.setlocoption (moption); /*set the listener to listen to the address information sent by the server .*/Mlocationclient.registerlocationlistener (NewBdlocationlistener () {@Override Public voidonreceivelocation (bdlocation bdlocation) {if(Bdlocation = =NULL)                    return; StringBuffer SB=NewStringBuffer (256); /*get latitude and longitude*/Latitude=Bdlocation.getlatitude (); Longitude=Bdlocation.getlongitude (); Sb.append ("Latitude:" +latitude+ "\ n"); Sb.append ("Longitude:" +longitude);                Show_position.settext (Sb.tostring ());            Mlocationclient.stop ();        }        }); Getpositiononce.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {if(Mlocationclient = =NULL)                    return;            Mlocationclient.start ();    }        }); } @Override Public voidOnDestroy () {if(Mlocationclient! =NULL&&mlocationclient.isstarted ())            {mlocationclient.stop (); Mlocationclient=NULL; }        Super. OnDestroy (); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (r.menu.my, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); }}

Android Studio configuration using Baidu API (with simple example)

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.