Android SDK for Baidu Map

Source: Internet
Author: User

The Baidu map SDK provides developers with convenient retrieval services. Today, I will introduce you to Poi search.
First, we need to build a basic map application. For details, see: Android sdk
On the basis of this project, we make some changes.
Step 1: Modify the layout file, add the keyword input box, and click to perform the search operation. The Code is as follows:
<RelativeLayout 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 = ". MainActivity">
 
<! -- Add mapview to Baidu map -->
<Com. baidu. mapapi. map. MapView android: id = "@ + id/bmapsView"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: clickable = "true"/>
 
<! -- Text box for keyword input by the user -->
<EditText
Android: id = "@ + id/editText1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentLeft = "true"
Android: layout_alignParentTop = "true"
Android: layout_toLeftOf = "@ + id/button1"
Android: hint = "enter a search keyword"
Android: EMS = "50">
<RequestFocus/>
</EditText>
 
<! -- Execute the Poi Search button -->
<Button
Android: id = "@ + id/button1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentRight = "true"
Android: layout_alignParentTop = "true"
Android: text = "Search for Poi"/>
 
</RelativeLayout>

Step 2: Define the EditText and Button objects in the main class and initialize them. The Code is as follows:

// Initialize the keyword input box and button control
EditText = (EditText) findViewById (R. id. editText1 );
Button = (Button) findViewById (R. id. button1 );

Step 3: define and initialize the search listening object (Here we only listen to poi search. If developers use other searches, they only need to modify the corresponding listening method ). The Code is as follows:

MKSearchListener mkSearchListener = new MKSearchListener (){

@ Override
Public void onGetWalkingRouteResult (MKWalkingRouteResult arg0, int arg1 ){
// TODO Auto-generated method stub

}

@ Override
Public void onGetTransitRouteResult (MKTransitRouteResult arg0, int arg1 ){
// TODO Auto-generated method stub

}

@ Override
Public void onGetSuggestionResult (MKSuggestionResult arg0, int arg1 ){
// TODO Auto-generated method stub

}

@ Override
Public void onGetPoiResult (MKPoiResult arg0, int arg1, int arg2 ){
// TODO Auto-generated method stub
// First, determine whether the search result is found.
If (arg2! = 0 | arg0 = null)
{
Toast. makeText (MainActivity. this, "no result found! ", Toast. LENGTH_SHORT). show ();
Return;
}
// Draw the result to the map
If (arg0.getCurrentNumPois ()> 0)
{
PoiOverlay poiOverlay = new PoiOverlay (MainActivity. this, mapView );
PoiOverlay. setData (arg0.getAllPoi ());
MapView. getOverlays (). clear ();
MapView. getOverlays (). add (poiOverlay );
MapView. refresh ();
// When arg1 is 2 (bus line) or 4 (subway line), the poi coordinate is empty
For (MKPoiInfo info: arg0.getAllPoi ())
{
If (info.pt! = Null ){
MapView. getController (). animateTo (info.pt );
Break;
}
}
}
}

@ Override
Public void onGetPoiDetailSearchResult (int arg0, int arg1 ){
// TODO Auto-generated method stub

}

@ Override
Public void onGetDrivingRouteResult (MKDrivingRouteResult arg0, int arg1 ){
// TODO Auto-generated method stub

}

@ Override
Public void onGetBusDetailResult (MKBusLineResult arg0, int arg1 ){
// TODO Auto-generated method stub

}

@ Override
Public void onGetAddrResult (MKAddrInfo arg0, int arg1 ){
// TODO Auto-generated method stub

}
};

Step 4: Define the Poi search object and initialize it. The Code is as follows:

// Initialize the Poi search object
MkSearch = new MKSearch ();
MkSearch. init (bMapManager, mkSearchListener );

Step 5: Set the button click event to implement Poi search. The Code is as follows:

Button. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
String key = editText. getText (). toString ();
// If the keyword is null, no search is performed.
If (key. equals (""))
{
Toast. makeText (MainActivity. this, "Enter the search keyword! ", Toast. LENGTH_SHORT). show ();
}
Else
{
// Here, Poi search takes city-wide Poi search as an example. developers can flexibly use it based on their actual needs.
MkSearch. descriearchincity ("Beijing", key );
}
}
});

Step 6: complete! The result is shown in:

 

 

 

Note: This example only shows the most basic core code. To view all the code, download the original project file.
In addition, this simple example aims to illustrate the most basic usage and execution logic of Poi. Baidu map SDK provides developers with a wide range of search services, you can perform city search, peripheral search, and range search.

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.