Use Baidu api for Android Studio configuration (with a simple example)

Source: Internet
Author: User

Use Baidu api for Android Studio configuration (with a simple example)
I still want to use the api of Baidu map for the course assignment project I developed with my classmates. However, the official documentation seems to be only an example of Eclipse, which does not seem to be explained to Android Studio. is it because the latter is a "DoCoMo" product? It's easy to use APIs. Fields and methods of the class. The official documentation has provided details. Before using APIs for your app, you need to apply for a key on the baidu map developer website to bind your app. Otherwise, the server will not handle you. I will not go into details about these official documents. In the above sdk, select a version for download. I will download the latest version 5.1 and get a folder BaiduLBS_AndroidSDK_Lib. The directory structure is as follows: There are two issues left: 1. how to configure the jar and so files in the downloaded sdk into Android Studio 2. how to configure Manifest solution: 1. put the jar file in the libs directory of the Project structure: Right-click the jar file and add it to the library (add as libraries). Then, create a sub-directory under libs: armeabi, put the so file in this subdirectory. 2. androidManifest. xml configuration, which is available in the demo of the official documentation. But if you are too lazy, you can look down and there are not many changes. Add the sub-element <uses-permission android: name = "android. permission. BAIDU_LOCATION_SERVICE "> </uses-permission> <uses-permission android: name =" android. permission. ACCESS_COARSE_LOCATION "> </uses-permission> <uses-permission android: name =" android. permission. ACCESS_FINE_LOCATION "> </uses-permission> <uses-permission android: name =" android. permission. ACCESS_WIFI_STATE "> </uses-permission> <uses-permission Android: name = "android. permission. ACCESS_NETWORK_STATE "> </uses-permission> <uses-permission android: name =" android. permission. CHANGE_WIFI_STATE "> </uses-permission> <uses-permission android: name =" android. permission. READ_PHONE_STATE "> </uses-permission> <uses-permission android: name =" android. permission. WRITE_EXTERNAL_STORAGE "> </uses-permission> <uses-permission android: name =" android. permission. INTERNET "> </Uses-permission> <uses-permission android: name =" android. permission. MOUNT_UNMOUNT_FILESYSTEMS "> </uses-permission> <uses-permission android: name =" android. permission. READ_LOGS "> </uses-permission> in the application, add the sub-element: <service android: name =" com. baidu. location. f "android: enabled =" true "android: process =": remote "> <intent-filter> <action android: name =" com. baidu. location. service_v2.2 "> </action> </I Ntent-filter> </service> <! -- Meta-data needs to be written in application --> <meta-data android: name = "com. baidu. lbsapi. API_KEY "android: value =" Enter the applied key "/> is it strange to see the configuration file? But it worked. now, you can use the api to write code normally. here is a simple example. Its function is to obtain the current longitude and latitude every time you click a button. xml: <LinearLayout xmlns: 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 "> <TextView android: id =" @ + id/show_position "android: text =" @ string/hello_world "android: layout_width = "Wrap_content" android: layout_height = "wrap_content"/> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "horizontal" android: gravity = "center_horizontal"> <Button android: 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: package com. example. user. testmap; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. view; import android. widget. button; import android. widget. textView; import com. baidu. location. BDLocation; import com. baidu. location. BDLocationListener; import com. baidu. location. locationClient; import com. baid U. location. locationClientOption; public class MyActivity extends Activity {public TextView show_position; public Button getPositionOnce; private LocationClient mLocationClient; private parameter mOption; public double Latitude, longpolling; @ Override protected void onCreate (Bundle parent) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_my); show_positio N = (TextView) findViewById (R. id. show_position); getPositionOnce = (Button) findViewById (R. id. control_button); mLocationClient = new LocationClient (this); mOption = new LocationClientOption ();/* set Option */mOption. setOpenGps (true); mOption. setCoorType ("bd09ll"); mOption. setScanSpan (100); // scan once every 0.1 s (which should be the meaning of satellite positioning)/* set the Option on the local Client side */mLocationClient. setLocOption (mOption);/* sets the listener and listens to the server Sent address information */mLocationClient. registerLocationListener (new BDLocationListener () {@ Override public void onReceiveLocation (BDLocation bdLocation) {if (bdLocation = null) return; StringBuffer sb = new StringBuffer (256 ); /* obtain the longitude and Latitude */Latitude = bdLocation. getLatitude (); longdistance = bdLocation. getlongpolling (); sb. append ("latitude:" + Latitude + "\ n"); sb. append ("longpolling:" + longpolling); show_position.setTex T (sb. toString (); mLocationClient. stop () ;}}); getPositionOnce. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {if (mLocationClient = null) return; mLocationClient. start () ;}}) ;}@ Override public void onDestroy () {if (mLocationClient! = Null & mLocationClient. isStarted () {mLocationClient. stop (); mLocationClient = null;} super. onDestroy () ;}@ Override public boolean onCreateOptionsMenu (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 boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest. xml. int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}

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.