Android Learning location search and tagging based on Baidu map

Source: Internet
Author: User

In today's life, the map is more and more important, as a normal programmer, we can not write a map ourselves, so this article is about how to use Baidu's third-party framework to write their own programs,

First we need to become Baidu Developer, and then download the relevant SDK, Baidu search Baidu Map, and then enter the Baidu API home, and then click Into, here I only say some points to note, first, in the creation of applications, we should choose is ANDROIDSDK, Then the Android SHA1 fingerprint can be obtained through the windows->preference->android->build in eclipse, after the input package name can get access to the application AK, Here I say the download of the SDK, after downloading the SDK, there may be different versions of the situation, but the general situation should be similar, we need to copy the SDK directory Armeabi file under the Libs of our project, and then copy the jar file to our Libs directory, Add some jar files to BuildPath, and then in the image below, we need to tick our catalogue and confirm that the private Libraries is checked.

To here, our basic environment, even if it is set up, the rest can basically follow the guidance of Baidu map to do, here I paste my code:

Configuration file

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/        Android "package=" Com.example.demo "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk    android:minsdkversion= "android:targetsdkversion="/> <!--ushyzsgq3vhkpwxeerfxj9pg4buf14qm-- <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:na Me= "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"/> <application android:allowbackup= "true"  android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" > <meta-data android:name= "Com.baidu.lbsapi.API_KEY" android:value= "Ushyzsgq3vhkpwxeer Fxj9pg4buf14qm "/> <activity android:name=" com.example.demo.MainActivity "Android:label = "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MA In "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter&        Gt </activity> </application></manifest>
Main interface Code
<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 "Android:orien tation= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_ Vertical_margin "tools:context=". Mainactivity "> <linearlayout android:layout_width=" match_parent "android:layout_height=" Wrap_conte NT "android:orientation=" horizontal "> <edittext android:id=" @+id/address "Andro Id:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "2" Android            oid:text= "Sichuan University"/> <button android:id= "@+id/btn_search" android:layout_width= "0DP" Android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "search"/> &LT;/LINEARLAYOUT&G    T <linearlayout android:id= "@+id/map_container" android:layout_width= "Match_parent" Android:layout_h eight= "match_parent" android:orientation= "vertical" > </LinearLayout></LinearLayout>
Activity Code
Package Com.example.demo;import Com.baidu.mapapi.sdkinitializer;import Com.baidu.mapapi.map.baidumap;import Com.baidu.mapapi.map.bitmapdescriptor;import Com.baidu.mapapi.map.bitmapdescriptorfactory;import Com.baidu.mapapi.map.mapstatusupdate;import Com.baidu.mapapi.map.mapstatusupdatefactory;import Com.baidu.mapapi.map.mapview;import Com.baidu.mapapi.map.marker;import Com.baidu.mapapi.map.markeroptions;import Com.baidu.mapapi.model.latlng;import Com.baidu.mapapi.search.core.searchresult;import Com.baidu.mapapi.search.geocode.geocodeoption;import Com.baidu.mapapi.search.geocode.geocoderesult;import Com.baidu.mapapi.search.geocode.geocoder;import Com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener; Import Com.baidu.mapapi.search.geocode.reversegeocoderesult;import Android.os.bundle;import android.app.Activity; Import Android.text.inputfilter.lengthfilter;import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import anDroid.widget.edittext;import Android.widget.linearlayout;import Android.widget.toast;public class MainActivity Extends Activity {//Declare a container to put Mapviewlinearlayout map_container;//declare a query button btn_search;//declare an input box with an input position edittext et _location;//declares a token marker marker;//gets a geocoding geocoder mgeocoder;//declares a baidumap used to control the map Baidumap mbaidumap;// Get the view of the map Mapview mv; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Sdkinitializer.initialize (Getapplicationcontext ()); init ();} private void Init () {//Get view container Map_container = (linearlayout) Findviewbyid (R.id.map_container);//Get Mapviewmapview MV = n EW Mapview (this);//Add map to the View container Map_container.addview (MV),//through Mapview to get Baidumapmbaidumap = Mv.getmap ();//Get input Box et_ Location = (EditText) Findviewbyid (r.id.address);//Get Search button Btn_search = (button) Findviewbyid (r.id.btn_search);// Add a Click event for the button Btn_search.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//Implement Click event//Get Gercoder Object mgeocoder = Geocoder.newinstance () by GeoCoder instance method;//Get Gencodeoption object geocodeoption Mgeocodeoption = new Geocodeoption ();//Gets the contents of the input box Mgeocodeoption.address (Et_location.gettext (). toString ());// The city must be set up here, otherwise it will error mgeocodeoption.city ("Chengdu");//Set the Listener event for Geocoder Mgeocoder.setongetgeocoderesultlistener (new Ongetgeocoderresultlistener () {//This method is to convert coordinates to a specific address @overridepublic void Ongetreversegeocoderesult ( Reversegeocoderesult arg0) {}//Converts the specific address into coordinates @overridepublic void Ongetgeocoderesult (Geocoderesult arg0) {//Program robustness judgment if ( arg0 = = null| | Arg0.error! = SearchResult.ERRORNO.NO_ERROR) {toast.maketext (mainactivity.this, "Retrieval Error", Toast.length_short). Show (); else {//get the coordinates of the specific address latlng pos = arg0.getlocation ();//Get a tagged controller markeroptions mmarkeroptions = new Markeroptions ();//We set Bitmapdescriptor mbitmapdescriptor = Bitmapdescriptorfactory.fromresource (r.drawable.ic_launcher) is required when marking a parameter. Set the icon for the marker Mmarkeroptions.icon (mbitmapdescriptor);//Set the coordinates of the tag mmarkeroptions.position (POS);//Add Tag Mbaidumap.aDdoverlay (mmarkeroptions);//Set the parameters of the map jump mapstatusupdate mmapstatusupdate = Mapstatusupdatefactory.newlatlngzoom (POS, 15);//set for map jump Mbaidumap.setmapstatus (mmapstatusupdate);}}); /This sentence must be written, otherwise the listener event will not execute Mgeocoder.geocode (mgeocodeoption);}});}}


Android Learning location search and tagging based on Baidu map

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.