Baidu map API poi search-discover the points of interest around you, such as supermarkets, restaurants, ATM... (Android)

Source: Internet
Author: User

 This article mainly explains how to search for poi near a specified location through Baidu map API (Point of interest, point of interest), Such as supermarkets, restaurants, ATMs, and KTVs.
Although in the previous article, when querying address information based on latitude and longitude, some poi around the address will also be obtained, but those poi are uncertain (may be restaurants, banks, bars, it may also be schools, hospitals, supermarkets, and most likely not the places we are interested in.
I think everyone may have had this experience: carrying a bank card in his pocket, he finds an ATM everywhere on the street ). In this scenario, ATM is a point of interest. What we want to do is to find some nearby ATMs and withdraw money. At this time, we do not care about supermarkets and bars nearby, because there is no way to get money from these places!
So much has been said, on the one hand, to deepen everyone's understanding of the word poi, on the other hand, to let everyone understand what we will do next. There will be no more theoretical things. Let's look at the example.
1) Layout file Res/layout/Main. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <COM. baidu. mapapi. mapview Android: Id = "@ + ID/map_view" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: clickable = "true"/> </linearlayout>

2) inherits the activity class of COM. Baidu. mapapi. mapactivity.

Package COM. liufeng. baidumap; <br/> Import android. OS. bundle; <br/> Import COM. baidu. mapapi. bmapmanager; <br/> Import COM. baidu. mapapi. geopoint; <br/> Import COM. baidu. mapapi. mkaddrinfo; <br/> Import COM. baidu. mapapi. mkdrivingrouteresult; <br/> Import COM. baidu. mapapi. mkpoiresult; <br/> Import COM. baidu. mapapi. mksearch; <br/> Import COM. baidu. mapapi. mksearchlistener; <br/> Import COM. baidu. mapapi. mktransitr Outeresult; <br/> Import COM. baidu. mapapi. mkwalkingrouteresult; <br/> Import COM. baidu. mapapi. mapactivity; <br/> Import COM. baidu. mapapi. mapcontroller; <br/> Import COM. baidu. mapapi. mapview; <br/> Import COM. baidu. mapapi. poioverlay; public class extends earchactivity extends mapactivity {<br/> // defines the map engine management class <br/> private bmapmanager mapmanager; <br/> // define the search service class <br/> private mksearch mmksearch; <br/> privat E mapview; <br/> private mapcontroller; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> // initialize mapactivity <br/> mapmanager = new bmapmanager (getapplication ()); <br/> // set the first parameter of the init Method to the requested API <br/> keymapmanager. init ("285b450ebab2a92293e85502150ada7f03c777c4", null); <br /> Super. initmapactivity (mapmanager); <br/> mapview = (mapview) findviewbyid (R. id. map_view); <br/> // set the map mode to traffic Map <br/> mapview. settraffic (true); <br/> // set to enable the built-in zoom control <br/> mapview. setbuiltinzoomcontrols (true); <br/> // construct a latitude and longitude point (at the gate of Guizhou University) <br/> geopoint = new geopoint (INT) (26.449446*1e6 ), (INT) (106.682949*1e6); <br/> // gets the map controller object, used to control mapview <br/> mapcontroller = mapview. getcontro Roller (); <br/> // set the center of the Map <br/> mapcontroller. setcenter (geopoint); <br/> // sets the default map zoom level <br/> mapcontroller. setzoom (16); <br/> // initialize mksearch <br/> mmksearch = new mksearch (); <br/> mmksearch. init (mapmanager, new mysearchlistener (); <br/> // search for ATMs within 500 meters near the entrance of Guizhou University <br/> mmksearch. poisearchnearby ("ATM", geopoint, 500); <br/>}< br/> @ override <br/> protected Boolean isroutedisplayed () {<br/> retu Rn false; <br/>}< br/> @ override <br/> protected void ondestroy () {<br/> If (mapmanager! = NULL) <br/>{< br/> // call this method before exiting the Program <br/> mapmanager. destroy (); <br/> mapmanager = NULL; <br/>}< br/> super. ondestroy (); <br/>}< br/> @ override <br/> protected void onpause () {<br/> If (mapmanager! = NULL) <br/>{< br/> // terminate Baidu Map <br/> apimapmanager. stop (); <br/>}< br/> super. onpause (); <br/>}< br/> @ override <br/> protected void onresume () {<br/> If (mapmanager! = NULL) {<br/> // enable Baidu Map <br/> apimapmanager. start (); <br/>}< br/> super. onresume (); <br/>}< br/>/*** implements the mksearchlistener interface, used to Implement Asynchronous search service <br/> ** @ author Liufeng <br/> */<br/> public class mysearchlistener implements mksearchlistener {<br/>/*** Based on longitude and latitude search address information result <br/> ** @ Param result search result <br/> * @ Param ierror error code (0 indicates a correct return) <br/> */<br/> @ override <br/> Public void ongetaddrresult (mkaddrinfo result, int ierror) {<br/>}< br/>/*** driving route search result <br/> ** @ Param result search result <br/> * @ Param ierror code (0 indicates that the returned result is correct) <br/> */<br/> @ override <br/> Public void ongetdrivingrouteresult (mkdrivingrouteresult result, int ierror) {<br/>}< br/>/*** poi search results (range search, city poi search, and surrounding search) <br/> ** @ Param result search result * @ Param type return result type (, 21: poi list 7: City list) <br/> * @ Param ierror No. (0 indicates correct return) <br/> */<br/> @ override <br/> Public void ongetpoiresult (mkpoiresult result, int type, int ierror) {<br/> If (result = NULL) <br/>{< br/> return; <br/>}< br/> // poioverlay is an overlaypoioverlay provided by the Baidu map API for displaying POI. <br/> poioverlay = new poioverlay (cancearchactivity. this, mapview); <br/> // set the searched poi data <br/> poioverlay. setdata (result. getallpoi (); <br/> // display poioverlay on the map (add the points of interest to the map) <br/> mapview. getoverlays (). add (poioverlay ); <br/>}< br/>/*** search result of public transit transfer route <br/> ** @ Param result <br/> * @ Param ierror code (0 indicates that the returned result is correct) <br/> */<br/> @ override <br/> Public void ongettransitrouteresult (mktransitrouteresult result, int ierror) {} <br/>/*** walking Route Search Result <br/> ** @ Param result search result <br/> * @ Param ierror error no. (0 indicates a correct result is returned.) <br/> */<br/> @ override <br/> Public void ongetwalkingrouteresult (mkwalkingrouteresult result, int ierror ){}}}

3) configuration in androidmanifest. xml

Package COM. liufeng. baidumap; import android. OS. bundle; import COM. baidu. mapapi. bmapmanager; import COM. baidu. mapapi. geopoint; import COM. baidu. mapapi. mkaddrinfo; import COM. baidu. mapapi. mkdrivingrouteresult; import COM. baidu. mapapi. mkpoiresult; import COM. baidu. mapapi. mksearch; import COM. baidu. mapapi. mksearchlistener; import COM. baidu. mapapi. mktransitrouteresult; import COM. baidu. mapapi. mkwalkingrouteresul T; import COM. baidu. mapapi. mapactivity; import COM. baidu. mapapi. mapcontroller; import COM. baidu. mapapi. mapview; import COM. baidu. mapapi. poioverlay; public class extends earchactivity extends mapactivity {// defines the map engine management class private bmapmanager mapmanager; // defines the search service class private mksearch mmksearch; private mapview; private mapcontroller; @ overridepublic void oncreate (bundle savedinstancestate) {super. O Ncreate (savedinstancestate); setcontentview (R. layout. main); // initialize mapactivitymapmanager = new bmapmanager (getapplication (); // set the first parameter of the init Method to the requested API keymapmanager. init ("285b450ebab2a92293e85502150ada7f03c777c4", null); super. initmapactivity (mapmanager); mapview = (mapview) findviewbyid (R. id. map_view); // sets the map mode to traffic map mapview. settraffic (true); // set to enable the built-in zoom control mapview. setbuiltinzoomcontrols (true); // construct a latitude and longitude point (At the entrance of Guizhou University) geopoint = new geopoint (INT) (26.449446*1e6), (INT) (106.682949*1e6); // obtain the map controller object, used to control mapviewmapcontroller = mapview. getcontroller (); // set the map's central mapcontroller. setcenter (geopoint); // sets the default map zoom level mapcontroller. setzoom (16); // initialize mksearchmmksearch = new mksearch (); mmksearch. init (mapmanager, new mysearchlistener (); // search for a 500-meter ATM mmksearch near the entrance of Guizhou University. poisearchnearby ("ATM", geopoint, 500) ;}@ overrideprotected Boolean isroutedisplayed () {return false ;}@ overrideprotected void ondestroy () {If (mapmanager! = NULL) {// call this method mapmanager before exiting the program. destroy (); mapmanager = NULL;} super. ondestroy () ;}@ overrideprotected void onpause () {If (mapmanager! = NULL) {// terminate apimapmanager. Stop () ;}super. onpause () ;}@ overrideprotected void onresume () {If (mapmanager! = NULL) {// enable Baidu map apimapmanager. start ();} super. onresume ();}/*** implements the mksearchlistener interface, used to Implement Asynchronous search service ** @ author Liufeng */public class mysearchlistener implements mksearchlistener {/*** search address information result based on latitude and longitude ** @ Param result search result * @ Param ierror Error no. (0 indicates correct return) * // @ overridepublic void ongetaddrresult (mkaddrinfo result, int ierror) {}/*** driving route search result ** @ Param result search result * @ Param ierror code (0 indicates correct return) */@ overridepublic void ongetdrivingrouteresult (mkdrivingrouteresult result, int ierror) {}/*** poi search result (range search, city poi search, and peripheral search) ** @ Param result search result * @ Param type return result type (, 12, 21: poi list 7: City list) * @ Param ierror error code (0 indicates correct return) */@ overridepublic void ongetpoiresult (mkpoiresult result, int type, int ierror) {If (result = NULL) {return;} // poioverlay is the overlaypoioverlay poioverlay (New poioverlay) provided by the Baidu map API. this, mapview); // you can specify poi data poioverlay. setdata (result. getallpoi (); // display poioverlay on the map (mark the searched point of interest on the map) mapview. getoverlays (). add (poioverlay);}/*** public transit transfer Route Search Result ** @ Param result search result * @ Param ierror code (0 indicates a correct response) * // @ overridepublic void ongettransitrouteresult (mktransitrouteresult result, int ierror) {}/*** walking Route Search Result ** @ Param result search result * @ Param ierror error code (0 indicates correct return) */@ overridepublic void ongetwalkingrouteresult (mkwalkingrouteresult result, int ierror ){}}}

4) running result

We want to search for an ATM within 500 meters at the entrance of Guizhou University. We can see on the map that we found 5 points of interest (a, B, c, d, e), click any point of interest, the name of the point of interest is displayed.

Note:In this example, after searching for poi, we directly use the poioverlay prompted by the baibu map API to mark the point of interest on the map. You may need this: in addition to marking the searched poi on the map, I also want to get these poi information, for example, "name", "Address", and "latitude and longitude", this question has actually been answered in the previous article. I will mention it again here.
See row 135-136 of the receivearchactivity class. The Code is as follows:
// Set the searched poi data
Poioverlay. setdata (result. getallpoi ());
We call the getallpoi () method of the search result class, which returns the arraylist <mkpoiinfo>, that is, the poi information set. The mkpoiinfo class is a common Java class, let's take a look at the attributes in it:

/*** Mkpoiinfo is the interest point information class */public class mkpoiinfo {// poi name public string name; // poi Address Public String address; // poi City Public String city; // poi contact number Public String phonenum; // poi zip code Public String postcode; // poi type public int epoitype; // poi geographic coordinate (longitude and latitude) Public geopoint pt ;}

In this way, you only need to traverse the arraylist <mkpoiinfo> to obtain information about all poi. Complete!

Source: http://blog.csdn.net/lyq8479/article/details/6394998

 

 

 

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.