Bus Transfer Route Search and TransitOverlay
Here only provides java code, xml code reference: Android learning notes preliminary study Baidu map http://www.bkjia.com/kf/201203/121875.html
Important code:
[Java] MKPlanNode start = new MKPlanNode ();
// Start point: Tiananmen
Start.pt = new GeoPoint (int) (40.003834809598516*1E6 ),
(Int) (116.3263213634491*1E6 ));
// Set the map center
MapController. setCenter (start.pt );
MKPlanNode end = new MKPlanNode ();
// End point: Bird's Nest
End.pt = new GeoPoint (int) (39.99142*1E6), (int) (116.39026999999998*1E6 ));
// Set the driving route search policy, with time first, minimum cost, or minimum distance
/*
* EBUS_TIME_FIRST
* Public static final int EBUS_TIME_FIRST
* Public Transport retrieval policy constant: time first
* EBUS_TRANSFER_FIRST
* Public static final int EBUS_TRANSFER_FIRST
* Public Transport retrieval policy constant: minimum transfer
* Ebus_assist_first
* Public static final int ebus_assist_first
* Public Transport retrieval policy constant: Minimum walking distance
* EBUS_NO_SUBWAY
* Public static final int EBUS_NO_SUBWAY
* Public Transport retrieval policy constant: excluding the subway
*/
// Set a travel route search policy, with time first, minimum transfer, minimum walking distance, or no subway
MKSearch. setTransitPolicy (MKSearch. EBUS_TRANSFER_FIRST );
MKSearch. transitSearch ("Beijing", start, end); // The city name must be set.
MKPlanNode start = new MKPlanNode ();
// Start point: Tiananmen
Start.pt = new GeoPoint (int) (40.003834809598516*1E6 ),
(Int) (116.3263213634491*1E6 ));
// Set the map center
MapController. setCenter (start.pt );
MKPlanNode end = new MKPlanNode ();
// End point: Bird's Nest
End.pt = new GeoPoint (int) (39.99142*1E6), (int) (116.39026999999998*1E6 ));
// Set the driving route search policy, with time first, minimum cost, or minimum distance
/*
* EBUS_TIME_FIRST
* Public static final int EBUS_TIME_FIRST
* Public Transport retrieval policy constant: time first
* EBUS_TRANSFER_FIRST
* Public static final int EBUS_TRANSFER_FIRST
* Public Transport retrieval policy constant: minimum transfer
* Ebus_assist_first
* Public static final int ebus_assist_first
* Public Transport retrieval policy constant: Minimum walking distance
* EBUS_NO_SUBWAY
* Public static final int EBUS_NO_SUBWAY
* Public Transport retrieval policy constant: excluding the subway
*/
// Set a travel route search policy, with time first, minimum transfer, minimum walking distance, or no subway
MKSearch. setTransitPolicy (MKSearch. EBUS_TRANSFER_FIRST );
MKSearch. transitSearch ("Beijing", start, end); // The city name must be set.
Implement onGetTransitRouteResult (MKTransitRouteResult, int) of MySearchListener and display the search result:
[Java] @ Override
Public void onGetTransitRouteResult (MKTransitRouteResult result, int iError ){
If (result = null ){
Return;
}
TransitOverlay transitOverlay = new TransitOverlay (MyMapActivity. this, mMapView );
// Only one solution is shown as an example.
TransitOverlay. setData (result. getPlan (0 ));
MMapView. getOverlays (). add (transitOverlay );
}
@ Override
Public void onGetTransitRouteResult (MKTransitRouteResult result, int iError ){
If (result = null ){
Return;
}
TransitOverlay transitOverlay = new TransitOverlay (MyMapActivity. this, mMapView );
// Only one solution is shown as an example.
TransitOverlay. setData (result. getPlan (0 ));
MMapView. getOverlays (). add (transitOverlay );
}
API:
TransitSearch
Public int transitSearch (java. lang. String city, MKPlanNode start, MKPlanNode end)
Search bus routes.
Asynchronous function, which returns the onGetTransitRouteResult method notification in the MKSearchListener.
Parameters:
City-city name, used for retrieval in which city
Start-the start point of the search. You can specify the start point by using the keyword or coordinate.
End-the end point of the search. You can specify the end point by using the keyword or coordinate.
Return Value:
0 is returned for success; otherwise-1 is returned.
SetTransitPolicy
Public int setTransitPolicy (int policy)
Set route planning policies.
The parameter is a policy constant. Valid for next search
Parameters:
Policy-EBUS_TIME_FIRST: time first; EBUS_TRANSFER_FIRST: less transfer; ebus_assist_first: less walk; EBUS_NO_SUBWAY: Non-Subway
Return Value:
0 is returned for success; otherwise-1 is returned.
Specific implementation:
[Java] view plaincopyprint? Package xiaosi. baiduMap;
Import android. app. AlertDialog;
Import android. content. DialogInterface;
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. MKPlanNode;
Import com. baidu. mapapi. MKPoiInfo;
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;
Import com. baidu. mapapi. RouteOverlay;
Import com. baidu. mapapi. TransitOverlay;
Public class BaiduMapActivity extends MapActivity
{
/** Called when the activity is first created .*/
Private BMapManager mapManager = null;
Private String key = "1B79478DA01F7800AEA8602517A6D89B38151105 ";
Private MapView mapView = null;
Private MKSearch mKSearch;
Private MapController mapController = 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. mapView );
// Set to enable the built-in zoom control
MapView. setBuiltInZoomControls (true );
// Get control of mMapView, which can be used to control and drive translation and Scaling
MapController = mapView. getController ();
// Set the map zoom level
MapController. setZoom (12 );
MKSearch = new MKSearch ();
// Note that only one MKSearchListener is supported. The last setting prevails.
MKSearch. init (mapManager, new MySearchListener ());
MKPlanNode start = new MKPlanNode ();
// Start point: Tiananmen
Start.pt = new GeoPoint (int) (40.003834809598516*1E6 ),
(Int) (116.3263213634491*1E6 ));
// Set the map center
MapController. setCenter (start.pt );
MKPlanNode end = new MKPlanNode ();
// End point: Bird's Nest
End.pt = new GeoPoint (int) (39.99142*1E6), (int) (116.39026999999998*1E6 ));
// Set the driving route search policy, with time first, minimum cost, or minimum distance
/*
* EBUS_TIME_FIRST
* Public static final int EBUS_TIME_FIRST
* Public Transport retrieval policy constant: time first
* EBUS_TRANSFER_FIRST
* Public static final int EBUS_TRANSFER_FIRST
* Public Transport retrieval policy constant: minimum transfer
* Ebus_assist_first
* Public static final int ebus_assist_first
* Public Transport retrieval policy constant: Minimum walking distance
* EBUS_NO_SUBWAY
* Public static final int EBUS_NO_SUBWAY
* Public Transport retrieval policy constant: excluding the subway
*/
// Set a travel route search policy, with time first, minimum transfer, minimum walking distance, or no subway
MKSearch. setTransitPolicy (MKSearch. EBUS_TRANSFER_FIRST );
MKSearch. transitSearch ("Beijing", start, end); // The city name must be set.
}
Public class MySearchListener implements MKSearchListener
{
Public void onGetAddrResult (MKAddrInfo arg0, int arg1)
{
/*
* Returns the address information search result. Parameter: arg0-arg1-error number of the search result. 0 indicates that the search result is correct and the result contains related information. 100 indicates that the search result is correct and there is no relevant address information.
*/
}
Public void onGetDrivingRouteResult (MKDrivingRouteResult arg0, int arg1)
{
/*
* Returns the driving route search result. Parameter: arg0-arg1-error no. In the search result, 0 indicates that the returned result is correct.
*/
}
Public void onGetPoiResult (MKPoiResult arg0, int arg1, int arg2)
{
/*
* Returns the poi search result. Parameter: arg0-search result arg1-return result type: MKSearch. TYPE_POI_LIST MKSearch. TYPE_AREA_POI_LIST MKSearch. TYPE_CITY_LIST arg2-error code. 0 indicates that the returned result is correct.
*/
}
Public void onGetTransitRouteResult (MKTransitRouteResult arg0, int arg1)
{
/*
* Return the bus search result. Parameter: arg0-arg1-error number of the search result. 0 indicates that a correct result is returned. When MKEvent is returned. ERROR_ROUTE_ADDR indicates that the start or end point is ambiguous. Call the getAddrResult method of MKTransitRouteResult to obtain the recommended start or end point information.
*/
If (arg0 = null ){
Return;
}
TransitOverlay transitOverlay = new TransitOverlay (BaiduMapActivity. this, mapView );
// Only one solution is shown as an example.
TransitOverlay. setData (arg0.getPlan (0 ));
MapView. getOverlays (). add (transitOverlay );
}
Public void onGetWalkingRouteResult (MKWalkingRouteResult arg0, int arg1)
{
/*
* Returns the walking route search result. Parameter: arg0-arg1-error no. In the search result, 0 indicates that the returned result is correct.
*/
}
}
@ 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 ();
}
}
From sunset hut