Android Baidu map development: automatically locates locations and fixed locations for driving, walking, and searching bus routes

Source: Internet
Author: User

Recently, I followed the Baidu map API to learn how to develop maps. First, I learned how to search for paths by car, bus, and walking at two points with known coordinates, later, I learned the positioning function to obtain the longitude and latitude of my location, however, when the two are combined to automatically obtain the longitude and latitude of their location, and then search for the path with a fixed location, the two cannot be obtained, because the first two types cannot obtain the longitude and latitude values in the two classes, they are written to the same class, and finally the longitude and latitude values are obtained and run. You need to get the latitude and longitude value in oncancelocation of BDLocationListener. Because you can get the latitude and longitude value directly, you can search for the path in it. Lori said something that I can only understand ....

Summarize the implementation process (mainly ):

1. initialize the variable

2. initialize BMapManager before setContentView.

3. obtain the MapController. Make sure that the center of the mapview is located at that point (this step is indispensable). If mapview is missing, the default address is displayed (which seems to be the location in Beijing) affects the search of the path at the start position.

4. Generate LoctionClient and use LocationClientOption to set it easily. Call locationClient. start ();
LocationClient. requestLocation (); locate and obtain the location, Use callback to register the listener with loopnclient, obtain the longitude and latitude in the callback method onReceiveLocation, and use MKPlanNode to set the start position (obtained longitude and latitude) and end point, you can initialize MKSearch, rewrite the callback Methods onGetWalkingRouteResult, onGetTransitRouteResult, onGetDrivingRouteResult In the parameter listener, and finally mkSearch. walkingSearch (null, satrt, null, end );
// MkSearch. drivingSearch (null, satrt, null, end );
// MkSearch. transitSearch ("Shanghai", satrt, end); (unable to run, waiting for learning)
You can search for routes.

The Code is as follows:

 

Package com. example. lockposi;

Import com. baidu. location. BDLocation;
Import com. baidu. location. BDLocationListener;
Import com. baidu. location. LocationClient;
Import com. baidu. location. LocationClientOption;
Import com. baidu. mapapi. BMapManager;
Import com. baidu. mapapi. MKGeneralListener;
Import com. baidu. mapapi. map. MKEvent;
Import com. baidu. mapapi. map. MapController;
Import com. baidu. mapapi. map. MapView;
Import com. baidu. mapapi. map. RouteOverlay;
Import com. baidu. mapapi. search. MKAddrInfo;
Import com. baidu. mapapi. search. MKBusLineResult;
Import com. baidu. mapapi. search. MKDrivingRouteResult;
Import com. baidu. mapapi. search. MKPlanNode;
Import com. baidu. mapapi. search. MKPoiInfo;
Import com. baidu. mapapi. search. MKPoiResult;
Import com. baidu. mapapi. search. MKSearch;
Import com. baidu. mapapi. search. MKSearchListener;
Import com. baidu. mapapi. search. MKSuggestionResult;
Import com. baidu. mapapi. search. MKTransitRouteResult;
Import com. baidu. mapapi. search. mkdomainingrouteresult;
Import com. baidu. platform. comapi. basestruct. GeoPoint;

Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. Menu;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. TextView;
Import android. widget. Toast;

Public class MainActivity extends Activity {

TextView textView;
LocationClient locationClient;
Double latitude, longpolling;
Private MapView mapview;
BMapManager bMapManager;
Static final String API_KEY = "07418aec69baab7425c6230a6120c580dffaeebb ";

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
BMapManager = new BMapManager (getApplicationContext ());
BMapManager. init (API_KEY, new MKGeneralListener (){

@ Override
Public void onGetPermissionState (int arg0 ){
// TODO Auto-generated method stub
If (arg0 = MKEvent. ERROR_PERMISSION_DENIED ){
Toast. makeText (getApplicationContext (), "enter the correct key", 1000). show ();

}
}

@ Override
Public void onGetNetworkState (int arg0 ){
// TODO Auto-generated method stub
If (arg0 = MKEvent. ERROR_NETWORK_CONNECT ){
Toast. makeText (getApplicationContext (), "your network has an error", 1000). show ();
}
}
});
SetContentView (R. layout. activity_main );
Mapview = (MapView) this. findViewById (R. id. bmapView );
Mapview. setBuiltInZoomControls (true );

MapController mMapController = mapview. getController ();

GeoPoint geoPoint = new GeoPoint (int) (31.243319*1E6 ),
(Int) (121.509075*1E6 ));
MMapController. setCenter (geoPoint );
MMapController. setZoom (12 );

TextView = (TextView) findViewById (R. id. textView1 );
LocationClient = new LocationClient (getApplicationContext ());
LocationClientOption locationClientOption = new LocationClientOption ();
LocationClientOption. setOpenGps (true );
LocationClientOption. setCoorType ("bd09ll ");
LocationClientOption. setPriority (LocationClientOption. NetWorkFirst );
LocationClientOption. setScanSpan (5000 );
LocationClient. setLocOption (locationClientOption );
LocationClient. start ();
LocationClient. requestLocation ();
Toast. makeText (getApplicationContext (), String. valueOf (latitude), 1000). show ();

LocationClient. registerLocationListener (new BDLocationListener (){

@ Override
Public void onReceivePoi (BDLocation arg0 ){
// TODO Auto-generated method stub
If (arg0 = null ){
Return;
}
Latitude = arg0.getLatitude ();
Longpolling = arg0.getlongpolling ();
StringBuilder sBuilder = new StringBuilder ();
SBuilder. append ("latitude:" + String. valueOf (arg0.getLatitude (). append ("longpolling:" + String. valueOf (arg0.getlongdistance ()));
TextView. setText (sBuilder. toString ());
}

@ Override
Public void onReceiveLocation (BDLocation arg0 ){
// TODO Auto-generated method stub
If (arg0 = null ){
Return;
}
Latitude = arg0.getLatitude ();
Longpolling = arg0.getlongpolling ();
StringBuilder sBuilder = new StringBuilder ();
SBuilder. append ("latitude:" + arg0.getLatitude (). append ("longdistance:" + arg0.getlongdistance ());
TextView. setText (sBuilder. toString ());
MKPlanNode satrt = new MKPlanNode ();
Satrt.pt = new GeoPoint (int) (31.205889*1E6 ),
(Int) (121.637942*1E6 ));
MKPlanNode end = new MKPlanNode ();
End.pt = new GeoPoint (int) (31.243319*1E6), (int) (121.509075*1E6 ));
Final MKSearch mkSearch = new MKSearch ();
MkSearch. init (bMapManager, new MKSearchListener (){

Private int intidx;

@ Override
Public void onGetWalkingRouteResult (MKWalkingRouteResult arg0, int arg1 ){
// TODO Auto-generated method stub
If (arg0 = null ){
Return;
}
RouteOverlay routeOverlay = new RouteOverlay (MainActivity. this, mapview );
RouteOverlay. setData (arg0.getPlan (0). getRoute (0 ));
Mapview. getOverlays (). add (routeOverlay );
Mapview. refresh ();
}

@ Override
Public void onGetTransitRouteResult (MKTransitRouteResult arg0, int arg1 ){
// TODO Auto-generated method stub
If (arg0 = null ){
Return;
}
RouteOverlay routeOverlay = new RouteOverlay (MainActivity. this, mapview );
RouteOverlay. setData (arg0.getPlan (0). getRoute (0 ));
Mapview. getOverlays (). add (routeOverlay );
Mapview. refresh ();

}

@ Override
Public void onGetSuggestionResult (MKSuggestionResult arg0, int arg1 ){
// TODO Auto-generated method stub

}

@ Override
Public void onGetPoiResult (MKPoiResult arg0, int arg1, int arg2 ){
// TODO Auto-generated method stub
If (arg2! = 0 | arg0 = null ){
Toast. makeText (MainActivity. this, "Sorry, no result found", Toast. LENGTH_LONG). show ();
Return;
}
// Find the bus route poi node
MKPoiInfo curPoi = null;
Int totalPoiNum = arg0.getNumPois ();
For (intidx = 0; intidx <totalPoiNum; intidx ++ ){
CurPoi = arg0.getPoi (intidx );
If (2 = curPoi. ePoiType ){
Break;
}
}
MkSearch. busLineSearch ("Shanghai", curPoi. uid );
}

@ Override
Public void onGetPoiDetailSearchResult (int arg0, int arg1 ){
// TODO Auto-generated method stub

}

@ Override
Public void onGetDrivingRouteResult (MKDrivingRouteResult arg0, int arg1 ){
// TODO Auto-generated method stub
If (arg0 = null ){
Return;
}
RouteOverlay routeOverlay = new RouteOverlay (MainActivity. this, mapview );
RouteOverlay. setData (arg0.getPlan (0). getRoute (0 ));
Mapview. getOverlays (). add (routeOverlay );
Mapview. refresh ();
}

@ Override
Public void onGetBusDetailResult (MKBusLineResult arg0, int arg1 ){
// TODO Auto-generated method stub
If (arg1! = 0 | arg0 = null ){
Toast. makeText (MainActivity. this, "Sorry, no result found", Toast. LENGTH_LONG). show ();
Return;
}
RouteOverlay routeOverlay = new RouteOverlay (MainActivity. this, mapview); // only one scheme is shown as an example.
RouteOverlay. setData (arg0.getBusRoute ());
Mapview. getOverlays (). clear ();
Mapview. getOverlays (). add (routeOverlay );
Mapview. refresh ();
Mapview. getController (). animateTo (arg0.getBusRoute (). getStart ());
}

@ Override
Public void onGetAddrResult (MKAddrInfo arg0, int arg1 ){
// TODO Auto-generated method stub

}
});

MkSearch. mongoingsearch (null, satrt, null, end );
// MkSearch. drivingSearch (null, satrt, null, end );
// MkSearch. transitSearch ("Shanghai", satrt, end );

}

});

 

}

 

}

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.