Baidu Map Android SDK-A simple example of using the search function

Source: Internet
Author: User

Baidu Map SDK not only for the vast number of developers to provide a cool map display effect, rich overlay layer, the vast number of developers to provide a variety of LBS retrieval capabilities. Through these interfaces, developers can easily access the LBS data of Baidu, enrich their mobile map application.

At present, the Baidu map SDK for developers to provide search services are:

POI Search: Can retrieve Baidu poi data information;

Route planning: Use Baidu's engine to help developers plan their routes;

Online suggestion search, short link sharing, geocoding, etc...

This article will take a very simple POI city within the search as an example, like the vast number of developers to introduce the use of the interface.

The first step, select and download the SDK;

This example we only use the search function, so we go to the Baidu LBS open platform of the Android unified resource download Platform download retrieval function module on it, the specific address is as follows: Http://lbsyun.baidu.com/sdk/download


The second step is to create the Android project and import the downloaded SDK into the project;

After the import is complete, the effect is as follows:


The third step is to design the interactive interface of the application;


Fourth step, add the required permissions and Key for SDK development in Androidmanifest;

<uses-permission android:name= "Android.permission.GET_ACCOUNTS"/> <uses-permission android:name= " Android.permission.USE_CREDENTIALS "/> <uses-permission android:name=" Android.permission.MANAGE_ACCOUNTS "/ > <uses-permission android:name= "Android.permission.AUTHENTICATE_ACCOUNTS"/> <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.CHANGE_WIFI_STATE "/> <uses-permission Android:name= "Android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name= " Android.permission.READ_PHONE_STATE "/> <uses-permission android:name=" android.permission.WRITE_EXTERNAL_ STORAGE "/> <uses-permission android:name=" Android.permission.BROADCAST_STICKY "/> <uses-permission Android:name= "Android.permissioN.write_settings "/> <uses-permission android:name=" Android.permission.READ_PHONE_STATE "/> 
<meta-data  android:name= "Com.baidu.lbsapi.API_KEY" android:value= "Gbm9sbm00c7cd6vfi2evzsgk"/>
Note that the key above is for the developer to change the key, please refer to the previous blog introduction.

Fifth step, programming;

Define three variables, Poisearch, poicitysearchoption, Ongetpoisearchresultlistener, respectively. It can be seen from these three names that these three variables are the listening methods for retrieving objects, retrieving parameters, and retrieving results, respectively.

Before performing a POI retrieval, the Poisearch object needs to be initialized by the following method:

Poisearch = Poisearch.newinstance ();
After the initialization is complete, you need to set up its listening method immediately, note that all the retrieval requests in the SDK are implemented asynchronously, the code is as follows:

Poisearch.setongetpoisearchresultlistener (Ongetpoisearchresultlistener);

After the preparation is complete, we add a real retrieval execution code in the button's click Response, as follows:

Button.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated Method stubpoicitysearchoption = new Poicitysearchoption (). City (City.gettext (). toString ()). Keyword (keyword.gettext (). toString ());p oisearch.searchincity (poicitysearchoption);}});
Add the implementation of the Listener method as follows:

Ongetpoisearchresultlistener Ongetpoisearchresultlistener = new Ongetpoisearchresultlistener () {@Overridepublic void Ongetpoiresult (Poiresult poiresult) {//TODO auto-generated method stubstring poiname = Poiresult.getallpoi (). Get (0). Name String Poiadd = Poiresult.getallpoi (). Get (0). Address; String idstring = Poiresult.getallpoi (). Get (0). Uid;textview.settext ("The first result is: \ n name =[" +poiname+ "]\nid = [" +idstring+ "] \ n Address =["+poiadd+"] "); @Overridepublic void Ongetpoidetailresult (Poidetailresult arg0) {//TODO auto-generated Method stub}};
As above, the listening method is to display the first message in the result, the execution of the program as the effect:



Here, we have implemented one of the simplest POI retrieval examples using the Baidu maps Android SDK. More functions Welcome to our official website to inquire, thank you!

Http://developer.baidu.com/map/index.php?title=androidsdk


Baidu LBS open platform to the vast number of developers to launch the development of the survey program, participate in our research and development testing, the first time to experience the function of our new products, to mention the BUG can also be awarded, is not very cool?! ~

For more information, please refer to: http://bbs.lbsyun.baidu.com/viewthread.php?tid=15300&extra=page%3D1

Baidu Map Android SDK-A simple example of using the search function

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.