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]
- MMKSearch. receivearchnearby (KFC, new GeoPoint (int) (39.915*1E6), (int) (116.404*1E6), 5000 );
Implement onGetPoiResult of MySearchListener and display the search result:
[Java]
- Public void onGetPoiResult (MKPoiResult result, int type, int iError ){
- If (result = null ){
- Return;
- }
- PoiOverlay poioverlay = new PoiOverlay (MyMapActivity. this, mMapView );
- Poioverlay. setData (result. getAllPoi ());
- MMapView. getOverlays (). add (poioverlay );
- }
Specific implementation:[Java]
- Package xiaosi. 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. MKWalkingRouteResult;
- Import com. baidu. mapapi. MapActivity;
- Import com. baidu. mapapi. MapController;
- Import com. baidu. mapapi. MapView;
- Import com. baidu. mapapi. PoiOverlay;
-
- Public class BaiduMapActivity extends MapActivity
- {
- /** Called when the activity is first created .*/
- Private BMapManager mapManager = null;
- Private String key = 1B79478DA01F7800AEA8602517A6D89B38151105;
- Private MapView mapView = null;
-
- @ Override
- Public void onCreate (Bundle savedInstanceState)
- {
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. main );
- MapManager = new BMapManager (getApplication ());
- MapManager. init (key, null );
- Super. initMapActivity (mapManager );
- MapView = (MapView) findViewById (R. id. mapsView );
- MapView. setBuiltInZoomControls (true); // you can enable the built-in zoom control.
- MapController mapController = mapView. getController (); // get control of mMapView, which can be used to control and drive translation and Scaling
- MapController. setZoom (12); // sets the map zoom level.
-
-
- MKSearch mKSearch = new MKSearch ();
- MKSearch. init (mapManager, new MySearchListener (); // note that only one MKSearchListener is supported. The last setting prevails.
- MKSearch. shortearchnearby (KFC, new GeoPoint (int) (39.915*1E6 ),
- (Int) (116.404*1E6), 5000 );
- }
-
- Public class MySearchListener implements MKSearchListener
- {
- Public void onGetAddrResult (MKAddrInfo arg0, int arg1)
- {}
-
- Public void onGetDrivingRouteResult (MKDrivingRouteResult arg0, int arg1)
- {}
-
- Public void onGetPoiResult (MKPoiResult arg0, int arg1, int arg2)
- {
- If (arg0 = null ){
- Return;
- }
- PoiOverlay poioverlay = new PoiOverlay (BaiduMapActivity. this, mapView );
- Poioverlay. setData (arg0.getAllPoi ());
- MapView. getOverlays (). add (poioverlay );
- }
-
- Public void onGetTransitRouteResult (MKTransitRouteResult arg0, int arg1)
- {}
-
- Public void onGetWalkingRouteResult (MKWalkingRouteResult arg0, int arg1)
- {}
- }
-
- @ Override
- Protected boolean isRouteDisplayed ()
- {
- Return false;
- }
-
- @ Override
- Protected void onDestroy ()
- {
- If (mapManager! = Null)
- {
- MapManager. destroy ();
- MapManager = null;
- }
- Super. onDestroy ();
- }
-
- @ Override
- Protected void onPause ()
- {
- If (mapManager! = Null)
- {
- MapManager. stop ();
- }
- Super. onPause ();
- }
-
- @ Override
- Protected void onResume ()
- {
- If (mapManager! = Null)
- {
- MapManager. start ();
- }
- Super. onResume ();
- }
- }