Android Baidu map API usage tutorial
Import warehouse receiving File
Download the latest library file on the download page. Copy the liblocSDK2.4.so file to the libs/armeabi directory. Copy the locSDK2.4.jar file to the project root directory, select "Add JARs" from Project Properties> Java Build Path> Libraries, select locSDK2.4.jar, and return the result after confirmation. In this way, you can use Baidu positioning API in the program.
Set AndroidManifest. xml
To differentiate services of Version 2.3, you must declare the intent filter in the manifest file as com. baidu. location. service_v2.4 and declare the service component in the application tag.
- Android: name = "com. baidu. location. f" android: enabled = "true" android: process = ": remote"
- Android: permission = "android. permission. BAIDU_LOCATION_SERVICE">
-
- Android: name = "com. baidu. location. service_v2.4">
-
-
Declare permission
- Android: name = "android. permission. BAIDU_LOCATION_SERVICE" >
- Android: name = "android. permission. BAIDU_LOCATION_SERVICE" >
- Android: name = "android. permission. ACCESS_COARSE_LOCATION" >
- Android: name = "android. permission. ACCESS_FINE_LOCATION" >
- Android: name = "android. permission. ACCESS_WIFI_STATE" >
- Android: name = "android. permission. ACCESS_NETWORK_STATE" >
- Android: name = "android. permission. CHANGE_WIFI_STATE" >
- Android: name = "android. permission. READ_PHONE_STATE" >
- Android: name = "android. permission. WRITE_EXTERNAL_STORAGE" >
- Android: name = "android. permission. INTERNET" />
- Android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS" >
- Android: name = "android. permission. READ_LOGS" >
Import-related classes
- ImportCom. baidu. location. BDLocation;
- ImportCom. baidu. location. BDLocationListener;
- ImportCom. baidu. location. LocationClient;
- ImportCom. baidu. location. LocationClientOption;
- ImportCom. baidu. location. bdpolicylistener;// If the location reminder function is used, the import class is required.
Use of function classes
Initialize the LocationClient class
Note that the LocationClient class must be declared in the main thread. Parameters of the Context type are required.
- PublicLocationClient mLocationClient =Null;
- PublicBDLocationListener myListener =NewMyLocationListener ();
-
- Public VoidOnCreate (){
- MLocationClient =NewLocationClient (This);// Declare the LocationClient class
- MLocationClient. registerLocationListener (myListener );// Register the listener Function
- }
Implement the BDLocationListener Interface
The BDLocationListener interface has two methods to implement:
1. Receive the location result returned asynchronously. The parameter is a BDLocation parameter.
2. Receive the result of the POI query returned asynchronously. The parameter is a BDLocation parameter.
- Public ClassMyLocationListennerImplementsBDLocationListener {
- @ Override
- Public VoidOncancelocation (BDLocation location ){
- If(Location =Null)
- Return;
- StringBuffer sb =NewStringBuffer (256 );
- Sb. append ("time :");
- Sb. append (location. getTime ());
- Sb. append ("\ NError code :");
- Sb. append (location. getLocType ());
- Sb. append ("\ NLatitude :");
- Sb. append (location. getLatitude ());
- Sb. append ("\ NLontitude :");
- Sb. append (location. getlongpolling ());
- Sb. append ("\ NRadius :");
- Sb. append (location. getRadius ());
- If(Location. getLocType () = BDLocation. TypeGpsLocation ){
- Sb. append ("\ NSpeed :");
- Sb. append (location. getSpeed ());
- Sb. append ("\ NSatellite :");
- Sb. append (location. getSatelliteNumber ());
- }Else If(Location. getLocType () = BDLocation. TypeNetWorkLocation ){
- Sb. append ("\ NAddr :");
- Sb. append (location. getAddrStr ());
- }
-
- LogMsg (sb. toString ());
- }
- Public VoidOnReceivePoi (BDLocation poiLocation ){
- If(PoiLocation =Null){
- Return;
- }
- StringBuffer sb =NewStringBuffer (256 );
- Sb. append ("Poi time :");
- Sb. append (poiLocation. getTime ());
- Sb. append ("\ NError code :");
- Sb. append (poiLocation. getLocType ());
- Sb. append ("\ NLatitude :");
- Sb. append (poiLocation. getLatitude ());
- Sb. append ("\ NLontitude :");
- Sb. append (poiLocation. getlongdistance ());
- Sb. append ("\ NRadius :");
- Sb. append (poiLocation. getRadius ());
- If(PoiLocation. getLocType () = BDLocation. TypeNetWorkLocation ){
- Sb. append ("\ NAddr :");
- Sb. append (poiLocation. getAddrStr ());
- }
- If(PoiLocation. hasPoi ()){
- Sb. append ("\ NPoi :");
- Sb. append (poiLocation. getPoi ());
- }Else{
- Sb. append ("noPoi information ");
- }
- LogMsg (sb. toString ());
- }
- }
Set parameters
Positioning parameters include: positioning mode (single positioning, timed positioning), return coordinate type, and whether to enable GPS. Eg:
- LocationClientOption option =NewLocationClientOption ();
- Option. setOpenGps (True);
- Option. setAddrType ("detail ");
- Option. setCoorType ("gcj02 ");
- Option. setScanSpan (5000 );
- Option. disableCache (True);// Disable cache locating
- Option. setPoiNumber (5 );// Maximum number of POI returned
- Option. setPoiDistance (1000 );// Poi query distance
- Option. setPoiExtraInfo (True);// Whether POI's phone number, address, and other details are required
- MLocClient. setLocOption (option );
Initiate a location request
Initiate a location request. The request process is asynchronous and the positioning result is obtained in the oncancelocation listener.
- If(MLocClient! =Null& MLocClient. isStarted ())
- MLocClient. requestLocation ();
- Else
- Log. d ("LocSDK_2.0_Demo1", "locClient is null or not started ");
Initiate a POI query request
Initiate a POI query request. The request process is asynchronous. The positioning result is obtained in the onReceivePoi listener.
- If(MLocClient! =Null& MLocClient. isStarted ())
- MLocClient. requestPoi ();
Location reminder
The location reminder can be sent three times at most, and will not be sent after three times. If you need to remind you again or modify the coordinates of the reminder point, you can use the setpolicylocation () function.
- // Location reminder code
- M1_yer =NewNotifyLister ();
- MNotifyer. SetNotifyLocation (42.03249652949337, 113.3129895882556, 3000, "gps ");// The four parameters represent the coordinates of the points to be reminded. The specific meanings are: latitude, longitude, distance range, and coordinate system type (gcj02, gps, bd09, bd09ll)
- MLocationClient. registerpolicy (mationyer );
- // After registering the location reminder listening event, you can use setpolicylocation to modify the location reminder settings, which will take effect immediately after modification.
- // BDNotifyListner implementation
- Public ClassNotifyListerExtendsBDNotifyListener {
- Public VoidOnNotify (BDLocation mlocation,FloatDistance ){
- MVibrator01.vibrate (1000 );// The vibration reminder has reached the specified position
- }
- }
- // Cancel the location reminder
- MLocationClient. removeNotifyEvent (mNotifyer );