Android Baidu Map Poi range Search _android

Source: Internet
Author: User
Tags event listener gettext ontap

I think you might have had the experience of having a bank card in your pocket and looking around the street for an ATM. In this scenario, ATM is the point of interest, we want to do is to find some of the nearest ATM and then withdraw money, at this time we do not care about the nearby supermarkets, bars, because these places do not have the means to take the cash!

Speaking so much, on the one hand is to deepen people's understanding of the word poi, on the other hand is to let people understand what we will do next. The theoretical stuff is no longer spoken, just take a look at the examples.

First to show you the effect of the picture:

Detailed interface:

This example mainly introduces the keyword query, suggestion query and view the dining class Place details page function, especially searches a local restaurant, barber shop and so on more practical significance, Baidu Demo code is as follows:

Activity:

Package com.home;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.text.Editable;
Import Android.text.TextWatcher;
Import Android.view.View;
Import Android.widget.ArrayAdapter;
Import Android.widget.AutoCompleteTextView;
Import Android.widget.EditText;
Import Android.widget.Toast;
Import Com.baidu.mapapi.BMapManager;
Import Com.baidu.mapapi.map.MapView;
Import Com.baidu.mapapi.search.MKAddrInfo;
Import Com.baidu.mapapi.search.MKBusLineResult;
Import Com.baidu.mapapi.search.MKDrivingRouteResult;
Import Com.baidu.mapapi.search.MKPoiInfo;
Import Com.baidu.mapapi.search.MKPoiResult;
Import Com.baidu.mapapi.search.MKSearch;
Import Com.baidu.mapapi.search.MKSearchListener;
Import Com.baidu.mapapi.search.MKShareUrlResult;
Import Com.baidu.mapapi.search.MKSuggestionInfo;
Import Com.baidu.mapapi.search.MKSuggestionResult;
Import Com.baidu.mapapi.search.MKTransitRouteResult;
Import Com.baidu.mapapi.search.MKWalkingRouteResult; /** * Demo POI search Function * * public class PoisearchactiviTy extends activity {private Mapview Mmapview = null; private Mksearch msearch = null;//Search module, can also remove map module use/** * search keyword Input
Window * * Private autocompletetextview Keyworldsview = null;
Private arrayadapter<string> sugadapter = null;
private int load_index;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
DemoApplication app = (demoapplication) this.getapplication ();
if (App.mbmapmanager = = null) {App.mbmapmanager = new Bmapmanager (this); App.mBMapManager.init (Demoapplication.strkey,
New Demoapplication.mygenerallistener ());
} setcontentview (R.layout.activity_poisearch);
Mmapview = (Mapview) Findviewbyid (R.id.bmapview);
Mmapview.getcontroller (). Enableclick (True);
Mmapview.getcontroller (). Setzoom (12);
Initialize the search module, register the search event listener Msearch = new Mksearch (); Msearch.init (App.mbmapmanager, New Mksearchlistener () {//This process details page results @Override public void Ongetpoidetailsearchresult ( int type, int error) {if (Error!= 0) {Toast.maketext (Poisearchactivity.this, "Sorry, no results found", Toast.length_short). Show ();
else {toast.maketext (poisearchactivity.this, "success, View Details page", Toast.length_short). Show ();} /** * This process POI search results/public void Ongetpoiresult (mkpoiresult res, int type, int error) {//error number can be referenced in the definition of mkevent if (Error!= 0 | | res = = null) {Toast.maketext (poisearchactivity.this, "Sorry, no results found", Toast.length_long). Show (); Move the map to the first POI center point if (Res.getcurrentnumpois () > 0) {//Show POI results to map mypoioverlay poioverlay = new Mypoioverlay (Poisearc
Hactivity.this, Mmapview, Msearch);
Poioverlay.setdata (Res.getallpoi ());
Mmapview.getoverlays (). Clear ();
Mmapview.getoverlays (). Add (Poioverlay);
Mmapview.refresh (); When Epoitype is 2 (bus line) or 4 (metro line), POI coordinates are NULL for (Mkpoiinfo Info:res.getAllPoi ()) {if (info.pt!= null) {Mmapview.getcontrol
Ler (). Animateto (info.pt);
Break else if (res.getcitylistnum () > 0) {//When the input keyword is not found in this city, but when found in other cities, returns a list of cities containing this keyword information String strinfo = "in"; for (int i = 0; I < Res.getcitylistnum (); i++) {Strinfo + = Res.getcitylistinFo (i). City;
Strinfo + = ",";
Strinfo + = "Find results";
Toast.maketext (Poisearchactivity.this, Strinfo, Toast.length_long). Show (); } public void Ongetdrivingrouteresult (mkdrivingrouteresult res, int error) {} public void Ongettransitrouteresult (Mktra Nsitrouteresult res, int error) {} public void Ongetwalkingrouteresult (mkwalkingrouteresult res. int error) {} public Vo 
ID Ongetaddrresult (mkaddrinfo res, int error) {} public void Ongetbusdetailresult (mkbuslineresult result, int ierror) {} /** * Update Suggestion list * * @Override public void Ongetsuggestionresult (mksuggestionresult res, int arg1) {if res = NULL | | res.ge
Tallsuggestions () = = null) {return;} sugadapter.clear (); For (Mksuggestioninfo info:res.getAllSuggestions ()) {if (Info.key!= null) sugadapter.add (Info.key);} sugadapter.notif
Ydatasetchanged ();
@Override public void Ongetshareurlresult (mkshareurlresult result, int type, int error) {}});
Keyworldsview = (Autocompletetextview) Findviewbyid (R.id.searchkey); Sugadapter = new ArraYadapter<string> (this, Android.)
R.layout.simple_dropdown_item_1line);
Keyworldsview.setadapter (Sugadapter); /** * When the input keyword changes, the dynamic Update proposal list/Keyworldsview.addtextchangedlistener (new Textwatcher () {@Override public void Aftertextchanged (Editable arg0) {} @Override public void beforetextchanged (charsequence arg0, int arg1, int arg2, int arg 3 {} @Override public void ontextchanged (charsequence cs, int arg1, int arg2, int arg3) {if (cs.length () <= 0) {RET
Urn
String City = ((EditText) Findviewbyid (r.id.city)). GetText (). toString ();
/** * Use the recommended search service to get a list of recommendations, and the results are updated in Onsuggestionresult ()/Msearch.suggestionsearch (Cs.tostring (), city);
}
}); @Override protected void OnPause () {mmapview.onpause (); Super.onpause ();} @Override protected void Onresume () {MMAPVI
Ew.onresume ();
Super.onresume (); @Override protected void OnDestroy () {Mmapview.destroy (); Super.ondestroy ();} @Override protected void onsaveinstances Tate (Bundle outstate) {super.onsaveinstancestate (outstate); Mmapview.onsaVeinstancestate (outstate); } @Override protected void Onrestoreinstancestate (Bundle savedinstancestate) {super.onrestoreinstancestate (
Savedinstancestate);
Mmapview.onrestoreinstancestate (savedinstancestate); /** * Impact Search Button Click event * * @param v/public void searchbuttonprocess (View v) {edittext editcity = (edittext) Findviewbyid (R
. id.city);
EditText Editsearchkey = (edittext) Findviewbyid (R.id.searchkey);
Msearch.poisearchincity (Editcity.gettext (). ToString (), Editsearchkey. GetText (). ToString ()); public void Gotonextpage (View v) {//Search next Group POI int flag = msearch.gotopoipage (++load_index); if (flag!= 0) {Toast.make
Text (Poisearchactivity.this, "search first, then search the next set of data", Toast.length_short). Show (); }
}
}

layout xml (activity_poisearch):

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical " > <linearlayout android:layout_width= "fill_parent" android:layout_height= "50dip" android:orientation= " Horizontal "> <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:text" = "in" > </TextView> <edittext android:id= "@+id/city" android:layout_width= "Wrap_content" Android:layout_ height= "Match_parent" android:text= "Beijing"/> <textview android:layout_width= "Wrap_content" Android:layout_ height= "Wrap_content" android:text= "City Look for" > </TextView> <autocompletetextview android:id= "@+id/searchkey" "Android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:layout_weight=" 0.88 "Android: text= "Restaurant"/> </LinearLayout> <linearlayout android:layout_width= "Fill_parent" Android:layout_height= "50dip" android:orientation= "horizontal" > <button android:id= "@+id/search" Fill_parent "android:layout_height=" wrap_content "android:layout_weight=" "android:background=" @drawable/button _style "android:onclick=" searchbuttonprocess "android:padding=" 10dip "android:text=" Start "/> <Button "@+id/map_next_data" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:layout_
weight= "android:background=" "@drawable/button_style" android:onclick= "Gotonextpage" android:padding= "10dip"
Android:text= "Next set of data"/> </LinearLayout> <com.baidu.mapapi.map.mapview android:id= "@+id/bmapview" Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" android:clickable= "true"/> </ Linearlayout>

Mypoioverlay class

Package com.home;
Import android.app.Activity;
Import Com.baidu.mapapi.map.MapView;
Import Com.baidu.mapapi.map.PoiOverlay;
Import Com.baidu.mapapi.search.MKPoiInfo;
Import Com.baidu.mapapi.search.MKSearch;
public class Mypoioverlay extends Poioverlay {
mksearch msearch;
Public Mypoioverlay (activity, Mapview Mapview, Mksearch search) {
super (activity, mapview);
Msearch = search;
}
@Override
protected Boolean onTap (int i) {
super.ontap (i);
Mkpoiinfo info = getpoi (i);
if (info.hascaterdetails) {
msearch.poidetailsearch (info.uid);
}
return true;
}

In the configuration file than before configuring a more activity, or can not see the detailed interface, which is provided in the Baidu SDK Jar class:

<activity
android:name= "com.baidu.mapapi.search.PlaceCaterActivity"
android:configchanges= " Orientation|keyboardhidden "
android:theme=" @android: Style/theme.notitlebar ">
</activity>

Application before it is similar.

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.