Android POI search, nearby search, and nearby search and positioning

Source: Internet
Author: User

Android POI search, nearby search, and nearby search and positioning

POI search can be performed in three ways: search within earchinbounds by range and search term, search within earchincity by city poi, and search around within earchnearby.

The following uses the peripheral search as an example to describe how to search and display the covering PoiOverlay:

 

Public intPoiSearchNearBy(Java. lang. String key, GeoPoint pt, int radius)

Initiate peripheral search based on the center point, radius, and search term.
 
Asynchronous function, which returns the onGetPoiResult method notification of the result in MKSearchListener.
Parameters:
Key-Keyword
Pt-central point geographic coordinates
Radius-radius, in meters
Return Value:
0 is returned. Otherwise,-1 Demo is returned. Search for the KFC restaurant within 5000 of Tiananmen Square.
[Java]
  1. MMKSearch. receivearchnearby (KFC, new GeoPoint (int) (39.915*1E6), (int) (116.404*1E6), 5000 );



    Implement onGetPoiResult of MySearchListener and display the search result:
    [Java]
    1. Public void onGetPoiResult (MKPoiResult result, int type, int iError ){
    2. If (result = null ){
    3. Return;
    4. }
    5. PoiOverlay poioverlay = new PoiOverlay (MyMapActivity. this, mMapView );
    6. Poioverlay. setData (result. getAllPoi ());
    7. MMapView. getOverlays (). add (poioverlay );
    8. }



      Specific implementation:[Java]
      1. Package xiaosi. baiduMap;
      2.  
      3. Import android. OS. Bundle;
      4.  
      5. Import com. baidu. mapapi. BMapManager;
      6. Import com. baidu. mapapi. GeoPoint;
      7. Import com. baidu. mapapi. MKAddrInfo;
      8. Import com. baidu. mapapi. MKDrivingRouteResult;
      9. Import com. baidu. mapapi. MKPoiResult;
      10. Import com. baidu. mapapi. MKSearch;
      11. Import com. baidu. mapapi. MKSearchListener;
      12. Import com. baidu. mapapi. MKTransitRouteResult;
      13. Import com. baidu. mapapi. MKWalkingRouteResult;
      14. Import com. baidu. mapapi. MapActivity;
      15. Import com. baidu. mapapi. MapController;
      16. Import com. baidu. mapapi. MapView;
      17. Import com. baidu. mapapi. PoiOverlay;
      18.  
      19. Public class BaiduMapActivity extends MapActivity
      20. {
      21. /** Called when the activity is first created .*/
      22. Private BMapManager mapManager = null;
      23. Private String key = 1B79478DA01F7800AEA8602517A6D89B38151105;
      24. Private MapView mapView = null;
      25.  
      26. @ Override
      27. Public void onCreate (Bundle savedInstanceState)
      28. {
      29. Super. onCreate (savedInstanceState );
      30. SetContentView (R. layout. main );
      31. MapManager = new BMapManager (getApplication ());
      32. MapManager. init (key, null );
      33. Super. initMapActivity (mapManager );
      34. MapView = (MapView) findViewById (R. id. mapsView );
      35. MapView. setBuiltInZoomControls (true); // you can enable the built-in zoom control.
      36. MapController mapController = mapView. getController (); // get control of mMapView, which can be used to control and drive translation and Scaling
      37. MapController. setZoom (12); // sets the map zoom level.
      38.  
      39.  
      40. MKSearch mKSearch = new MKSearch ();
      41. MKSearch. init (mapManager, new MySearchListener (); // note that only one MKSearchListener is supported. The last setting prevails.
      42. MKSearch. shortearchnearby (KFC, new GeoPoint (int) (39.915*1E6 ),
      43. (Int) (116.404*1E6), 5000 );
      44. }
      45.  
      46. Public class MySearchListener implements MKSearchListener
      47. {
      48. Public void onGetAddrResult (MKAddrInfo arg0, int arg1)
      49. {}
      50.  
      51. Public void onGetDrivingRouteResult (MKDrivingRouteResult arg0, int arg1)
      52. {}
      53.  
      54. Public void onGetPoiResult (MKPoiResult arg0, int arg1, int arg2)
      55. {
      56. If (arg0 = null ){
      57. Return;
      58. }
      59. PoiOverlay poioverlay = new PoiOverlay (BaiduMapActivity. this, mapView );
      60. Poioverlay. setData (arg0.getAllPoi ());
      61. MapView. getOverlays (). add (poioverlay );
      62. }
      63.  
      64. Public void onGetTransitRouteResult (MKTransitRouteResult arg0, int arg1)
      65. {}
      66.  
      67. Public void onGetWalkingRouteResult (MKWalkingRouteResult arg0, int arg1)
      68. {}
      69. }
      70.  
      71. @ Override
      72. Protected boolean isRouteDisplayed ()
      73. {
      74. Return false;
      75. }
      76.  
      77. @ Override
      78. Protected void onDestroy ()
      79. {
      80. If (mapManager! = Null)
      81. {
      82. MapManager. destroy ();
      83. MapManager = null;
      84. }
      85. Super. onDestroy ();
      86. }
      87.  
      88. @ Override
      89. Protected void onPause ()
      90. {
      91. If (mapManager! = Null)
      92. {
      93. MapManager. stop ();
      94. }
      95. Super. onPause ();
      96. }
      97.  
      98. @ Override
      99. Protected void onResume ()
      100. {
      101. If (mapManager! = Null)
      102. {
      103. MapManager. start ();
      104. }
      105. Super. onResume ();
      106. }
      107. }

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.