Android Baidu Map POI search function Instance code _android

Source: Internet
Author: User
Tags polyline lenovo

Before the introduction of the text to everyone to say what poi mean.

Due to the work of the relationship, often in the document will be seen in the poi of the three-letter abbreviation, but has been POI's concept and meaning has not been very detailed to study the meaning behind the representative. Today before work, and saw the POI these three letters, decided to seriously really search some poi specific meaning.

POI is the abbreviation of English, the original word is point of interest, literal translation into Chinese is the meaning of points of interest. The word "point of interest" originated from the navigation map manufacturer. In order to provide as much location information as possible, the map makers have spent a lot of effort to find destinations such as gas stations, restaurants, hotels, sights and so on, these destinations can be understood as one of the POI (points of interest). POI, in addition to referring to the destination's own information, such as the telephone, reviews the latitude and longitude of the destination, and can be displayed on an electronic map. This is POI, take the practical Google map as an example, we search Beijing Tian ' an door, there will be a pin-like things on the map to indicate the location of the door, click will see more information. In fact, this is a point of interest.

Why is the point of interest important? Now the popularity of electronic maps is increasing, there are no tens of thousands of points of interest, we can not easily search where we want to go. POI has become an indispensable part of electronic commerce and O2O now.

First, Introduction

POI (Point of interest), Chinese can be translated as "points of interest." In GIS, a POI can be a house, a shop, a mailbox, a bus stop, etc.

Android Baidu Map POI search Function Instance code

The Baidu Map SDK provides three types of POI searches: Peripheral search, area retrieval, and urban retrieval.

L Peripheral Search: to a certain point as the center, the designated distance for the radius, according to the user input keywords for poi search;

L Area Search: In the designated rectangular area, according to the keyword POI search;

L Urban Search: In a city, according to the user input keywords for poi search;

Starting from v3.6.1, the city POI retrieves the results of new Portal class list data. For example: In "Beijing" search "10 Street 1th", in addition to the return contains "1th on the ground 10 Street" POI list, also includes the address is "10 Street above" clear door.

Specifically, the Poisearch class searchincity (poicitysearchoption) initiates the retrieval by adding results that add to the Portal class data. A new getalladdr () Gets the list of gate classes in Poiresult, and when Ishasaddrinfo () returns True, the gate results are included in addition to the original POI list.

Android Baidu Map POI search Function Instance code

Poi details search means to retrieve the details of the point of interest based on the ID information of POI.

Android Baidu Map POI search Function Instance code

Online advice query refers to the keyword query online advice words. In order to help developers to achieve the search for the keyword quickly positioned on the map, the SDK from the 3.5.0 version, open the search results of the latitude and longitude of the information and corresponding POI point uid information.

Attention:

A. Online recommendations the essence of the search is based on the partial key is to retrieve the possible full keyword name, if you need these keywords corresponding to the POI specific information, please use POI search to complete;

B. The first article of online search results may have no latitude information, the results of the text associated with the keyword results, does not correspond to any exact poi point. For example, enter "Ken", the first result is "KFC", this result is a generic name, not with latitude and longitude information.

Second, the Operation screenshot

Introduction: Introduction of keyword query, suggestion query and view dining class Place Details page function

Detailed:

(1) Click on some keyword query results (such as "Restaurant") can jump to place details page;

(2) Provide suggestion query for Lenovo query, such as the importation of "Tiananmen Square" will be pop-up Lenovo query list;

This example runs the screenshot below:


Third, design steps

1. Add Demo12_poisearch.xml File

Add the file under the Layout folder, and then change the code to the following:

<?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 xmlns:android= "http://schemas.android.com/apk/res/android" 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= "Urban Search" > </ 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 xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_parent" android:layout_height= "50dip" android:orientation= "horizontal" > <button android:id= "@+id/search" Android: Layout_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_weight= "" android:background= "@
Drawable/button_style "android:padding=" 10dip "android:text=" Start "/> <button android:id=" @+id/map_next_data " Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:layout_weight= "Android": background= "@drawable/button_style" android:padding= "10dip" android:text= "next set of data"/> </LinearLayout> < Fragment android:id= "@+id/map" android:layout_width= "match_parent" android:layout_height= "Match_parent"  Com.baidu.mapapi.map.TextureMapFragment "/> </LinearLayout>

2. Add OverlayManager.cs File

Create a new Srcoverlayutil folder, and add the file under the folder.

Note: The files under the Srcoverlayutil folder are used to customize some of the advanced overlays that are based on the base coverage, including those used to display POI data, planning routes, bus detail routes, etc.

Using Com.Baidu.Mapapi.Map;
Using Com.Baidu.Mapapi.Model;
Using System.Collections.Generic;
namespace Bdmapv371demos.srcoverlayutil {///<summary>///provides a base class that can display and manage multiple overlay.
After the overlay Click event is passed to Overlaymanager, Overlaymanager can respond to the click event.
Handle the marker Click event in the Markerclick event. </summary> public abstract class OverlayManager:Java.Lang.Object, Baidumap.ionmarkerclicklistener, Baidumap.ionpolylineclicklistener {Baidumap mbaidumap = null; private list<overlayoptions> mOverlayOptionList =
Null
protected list<overlay> moverlaylist = null; Public Overlaymanager (Baidumap baidumap) {mbaidumap = Baidumap; Mbaidumap.setonmarkerclicklistener (this); if ( Moverlayoptionlist = = null) {moverlayoptionlist = new list<overlayoptions> ();} if (moverlaylist = = null) {Moverla
Ylist = new list<overlay> ();  }///<summary>///Override this method to set the overlay list to manage///</summary>///<returns></returns> Public abstract
List<overlayoptions> getoverlayoptions (); /// <summary>///Add all overlay to the map///</summary> public void Addtomap () {if (Mbaidumap = null) {return;}
Removefrommap ();
list<overlayoptions> overlayoptions = Getoverlayoptions (); if (overlayoptions!= null) {Moverlayoptionlist.addrange (getoverlayoptions ());} foreach (Overlayoptions option in MOver
Layoptionlist) {Moverlaylist.add (mbaidumap.addoverlay (option));}} <summary>///Remove all overlay from map///</summary> public void Removefrommap () {if (Mbaidumap = null) {retur
N foreach (Overlay marker in moverlaylist) {marker.
Remove ();
} moverlayoptionlist.clear ();
Moverlaylist.clear (); ///<summary>///Scale The map so that all the overlay are in the right field///Note: This method is only valid marker overlay for///types </summary> public void Zoomtosp A () {if (Mbaidumap = null) {return;} if (Moverlaylist.count > 0) {latlngbounds.builder Builder = new Latlngbounds .
Builder (); There may be too many points in the foreach (Overlay Overlay in moverlaylist) {//polyline, just press Marker to scale if (Overlay is Marker) {Builder. IncluDe (((Marker) overlay).
Position); } mbaidumap.setmapstatus (Mapstatusupdatefactory. Newlatlngbounds (builder.
Build ())); } public virtual bool Onmarkerclick (Marker Marker) {return false;} public virtual bool Onpolylineclick (polyline Polyli
NE) {return false;}}  }

3. Add PoiOverlay.cs File

Add the file under the Srcoverlayutil folder.

Using Android.os;
Using Android.widget;
Using Com.Baidu.Mapapi;
Using Com.Baidu.Mapapi.Map;
Using Com.Baidu.Mapapi.Search.Poi;
Using System.Collections.Generic; namespace Bdmapv371demos.srcoverlayutil {///<summary>///displays the overlay of a bus detail result, which inherits from subclasses of the class to display other types of overlay/// Summary> public class Poioverlay:overlaymanager {private static readonly int maxpoisize =; Private Poiresult Mpoi
result = NULL; Public Poioverlay (Baidumap baidumap): Base (Baidumap) {}///<summary>///set POI data///</summary>///<par Am Name= "Poiresult" >poi result data </param> public void SetData (Poiresult poiresult) {this.mpoiresult = Poiresult;} pu Blic override List<overlayoptions> Getoverlayoptions () {if (Mpoiresult = null | | mpoiresult.allpoi = NULL) {Retu
RN null;
} list<overlayoptions> markerlist = new list<overlayoptions> ();
int markersize = 0; for (int i = 0; i < mPoiResult.AllPoi.Count && markersize < maxpoisize; i++) {if (Mpoiresult.allpoI[i].
Location = = null) {continue;} markersize++;
Bundle Bundle = new Bundle (); Bundle.
Putint ("index", I); Markerlist.add (New Markeroptions (). Invokeicon (bitmapdescriptorfactory.fromassetwithdpi ("Icon_mark" + markersize +. png)). Invokeextrainfo (bundle). Invokeposition (Mpoiresult.allpoi[i].
Location));
return markerlist; 
///<summary>///Gets the POI data///</summary> public Poiresult Getpoiresult () {return mpoiresult of the Poioverlay; ///<summary>///Override this method to change the default click Behavior///</summary>///<param name= "I" > Clicked POI Index in Poiresult.allpoi </param>///<returns></returns> Public virtual bool Onpoiclick (int i) {if (Mpoiresult.allpoi!= null & amp;& Mpoiresult.allpoi[i]!= null) {Toast.maketext (Bmapmanager.context, Mpoiresult.allpoi[i]. Name, Toastlength.long).
Show ();
return false; public override bool Onmarkerclick (Marker Marker) {if (!moverlaylist.contains (Marker)) {return false;} if (Marker. ExtraInfo!= null) {return Onpoiclick (marker. EXtrainfo.getint ("index"));
return false;
public override bool Onpolylineclick (polyline polyline) {return false;}}  }

4. Add Demo12PoiSearch.cs File

Add the file under the Srcsdkdemos folder, and then change the code to the following:

Using Android.app;
Using Android.Content.PM;
Using Android.os;
Using ANDROID.SUPPORT.V4.APP;
Using Android.widget;
Using Bdmapv371demos.srcoverlayutil;
Using Com.Baidu.Mapapi.Map;
Using Com.Baidu.Mapapi.Search.Core;
Using Com.Baidu.Mapapi.Search.Poi;
Using Com.Baidu.Mapapi.Search.Sug; namespace Bdmapv371demos.srcsdkdemos {///<summary>///Demo poi search function///</summary> [activity (Label = @string/ Demo_name_poi ", configurationchanges = Configchanges.orientation | Configchanges.keyboardhidden, screenorientation = screenorientation.sensor)] public class Demo12poisearch: fragmentactivity {private Poisearch mpoisearch = null; private Suggestionsearch msuggestionsearch = null; private Baiduma
P mbaidumap = null;
Search for keyword input window private autocompletetextview Keyworldsview = null;
Private arrayadapter<string> sugadapter = null;
private int load_index = 0; protected override void OnCreate (Bundle savedinstancestate) {base.
OnCreate (savedinstancestate); Setcontentview (RESOURCE.LAYOUT.DEmo12_poisearch);
Initialize search module, register search event Monitor Mpoisearch = Poisearch.newinstance (); Mpoisearch.getpoiresult + = (s, e) => {var result = E.p0; if (result = NULL | |) Error = = SearchResult.ERRORNO.ResultNotFound) {return;} if, result.
Error = = SearchResult.ERRORNO.NoError) {mbaidumap.clear ();
Poioverlay overlay = new Mypoioverlay (this, mbaidumap);
Mbaidumap.setonmarkerclicklistener (overlay); Overlay.
SetData (result); Overlay.
Addtomap (); Overlay.
Zoomtospan ();
Return } if (result.
Error = = SearchResult.ERRORNO.AmbiguousKeyword) {//When the input keyword is not found in this city, but when found in other cities, returns a list of cities containing the keyword information string strinfo = "on"; foreach (Cityinfo cityinfo in result.
Suggestcitylist) {strinfo + = cityinfo.city; Strinfo + = ",";} Strinfo = "Find results"; Toast.maketext (This, Strinfo, Toastlength.long).
Show ();
}
}; Mpoisearch.getpoidetailresult + = (s, e) => {var result = E.p0; Error!= SearchResult.ERRORNO.NoError) {Toast.maketext (this, "Sorry, no results found", toastlength.short).
Show (); else {Toast.maketext (this, "success, View Details page"), Toastlength.short).
Show ();
}
};
Msuggestionsearch = Suggestionsearch.newinstance (); Msuggestionsearch.getsuggestionresult + = (s, e) => {var res = e.p0; if (res = = NULL | | res.)
Allsuggestions = = null) return;
Sugadapter.clear (); foreach (Suggestionresult.suggestioninfo info in res. Allsuggestions) {if (info). Key!= null) Sugadapter.add (info.
Key);
} sugadapter.notifydatasetchanged ();
};
Keyworldsview = findviewbyid<autocompletetextview> (Resource.Id.searchkey);
Sugadapter = new Arrayadapter<string> (this, Android.Resource.Layout.SimpleDropDownItem1Line);
Keyworldsview.adapter = Sugadapter;
Texturemapfragment map1 = fragmentmanager.findfragmentbyid<texturemapfragment> (Resource.Id.map); Mbaidumap = Map1.
Baidumap;
When the input keyword changes, the dynamic update proposal list keyworldsview.aftertextchanged + = (sender, E) => {};
keyworldsview.beforetextchanged + = (sender, E) => {};  keyworldsview.textchanged + = (sender, E) => {string s = e.text.tostring (); if (s.length <= 0) return; (Findviewbyid<edittext> (Resource.Id.city)).
Text; Get a list of suggestions using the recommended search service, and the result is to update msuggestionsearch.requestsuggestion (new Suggestionsearchoption () in Onsuggestionresult (). Keyword (s).
City);
};
Button btnsearch = findviewbyid<button> (Resource.Id.search);
Btnsearch.click + = delegate {searchbuttonprocess ();};
Button btnnext = findviewbyid<button> (Resource.Id.map_next_data);
Btnnext.click + = delegate {load_index++;
Searchbuttonprocess ();
}; } protected override void OnPause () {base.
OnPause (); } protected override void Onresume () {base.
Onresume (); protected override void OnDestroy () {Mpoisearch.destroy (); Msuggestionsearch.destroy (); base.
OnDestroy (); } protected override void Onsaveinstancestate (Bundle outstate) {base.
Onsaveinstancestate (outstate); } protected override void Onrestoreinstancestate (Bundle savedinstancestate) {base.
Onrestoreinstancestate (savedinstancestate); public void Searchbuttonprocess () {EditText editcity = findviewbyid<edittext> (resource.id. City);
EditText Editsearchkey = findviewbyid<edittext> (Resource.Id.searchkey); Mpoisearch.searchincity (New Poicitysearchoption (). City (Editcity.text). Keyword (Editsearchkey.text).
Pagenum (Load_index)); Private class Mypoioverlay:poioverlay {Demo12poisearch poisearchdemo; public Mypoioverlay (Demo12poisearch PoiSearchD Emo, Baidumap baidumap): Base (baidumap) {This.poisearchdemo = Poisearchdemo;} public override bool Onpoiclick (int index ) {base.
Onpoiclick (index); Poiinfo poi = Getpoiresult ().
Allpoi[index]; if (POI. Hascaterdetails) {PoiSearchDemo.mPoiSearch.SearchPoiDetail (New poidetailsearchoption ()). Poiuid (POI.
Uid));
return true;  }
}
}
}

5. Modify MainActivity.cs

In the Demos field definition of the MainActivity.cs file, remove the comment below "Example 12".

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.