Android Baidu map API usage tutorial

Source: Internet
Author: User

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.

 
  1. Android: name = "com. baidu. location. f" android: enabled = "true" android: process = ": remote"
  2. Android: permission = "android. permission. BAIDU_LOCATION_SERVICE">
  3. Android: name = "com. baidu. location. service_v2.4">
  4.  
  5.  

Declare permission

 
  1. Android: name = "android. permission. BAIDU_LOCATION_SERVICE" >
  2. Android: name = "android. permission. BAIDU_LOCATION_SERVICE" >
  3. Android: name = "android. permission. ACCESS_COARSE_LOCATION" >
  4. Android: name = "android. permission. ACCESS_FINE_LOCATION" >
  5. Android: name = "android. permission. ACCESS_WIFI_STATE" >
  6. Android: name = "android. permission. ACCESS_NETWORK_STATE" >
  7. Android: name = "android. permission. CHANGE_WIFI_STATE" >
  8. Android: name = "android. permission. READ_PHONE_STATE" >
  9. Android: name = "android. permission. WRITE_EXTERNAL_STORAGE" >
  10. Android: name = "android. permission. INTERNET" />
  11. Android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS" >
  12. Android: name = "android. permission. READ_LOGS" >
Import-related classes

 

 
  1. ImportCom. baidu. location. BDLocation;
  2. ImportCom. baidu. location. BDLocationListener;
  3. ImportCom. baidu. location. LocationClient;
  4. ImportCom. baidu. location. LocationClientOption;
  5. 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.

 
  1. PublicLocationClient mLocationClient =Null;
  2. PublicBDLocationListener myListener =NewMyLocationListener ();
  3.  
  4. Public VoidOnCreate (){
  5. MLocationClient =NewLocationClient (This);// Declare the LocationClient class
  6. MLocationClient. registerLocationListener (myListener );// Register the listener Function
  7. }
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.

 
  1. Public ClassMyLocationListennerImplementsBDLocationListener {
  2. @ Override
  3. Public VoidOncancelocation (BDLocation location ){
  4. If(Location =Null)
  5. Return;
  6. StringBuffer sb =NewStringBuffer (256 );
  7. Sb. append ("time :");
  8. Sb. append (location. getTime ());
  9. Sb. append ("\ NError code :");
  10. Sb. append (location. getLocType ());
  11. Sb. append ("\ NLatitude :");
  12. Sb. append (location. getLatitude ());
  13. Sb. append ("\ NLontitude :");
  14. Sb. append (location. getlongpolling ());
  15. Sb. append ("\ NRadius :");
  16. Sb. append (location. getRadius ());
  17. If(Location. getLocType () = BDLocation. TypeGpsLocation ){
  18. Sb. append ("\ NSpeed :");
  19. Sb. append (location. getSpeed ());
  20. Sb. append ("\ NSatellite :");
  21. Sb. append (location. getSatelliteNumber ());
  22. }Else If(Location. getLocType () = BDLocation. TypeNetWorkLocation ){
  23. Sb. append ("\ NAddr :");
  24. Sb. append (location. getAddrStr ());
  25. }
  26.  
  27. LogMsg (sb. toString ());
  28. }
  29. Public VoidOnReceivePoi (BDLocation poiLocation ){
  30. If(PoiLocation =Null){
  31. Return;
  32. }
  33. StringBuffer sb =NewStringBuffer (256 );
  34. Sb. append ("Poi time :");
  35. Sb. append (poiLocation. getTime ());
  36. Sb. append ("\ NError code :");
  37. Sb. append (poiLocation. getLocType ());
  38. Sb. append ("\ NLatitude :");
  39. Sb. append (poiLocation. getLatitude ());
  40. Sb. append ("\ NLontitude :");
  41. Sb. append (poiLocation. getlongdistance ());
  42. Sb. append ("\ NRadius :");
  43. Sb. append (poiLocation. getRadius ());
  44. If(PoiLocation. getLocType () = BDLocation. TypeNetWorkLocation ){
  45. Sb. append ("\ NAddr :");
  46. Sb. append (poiLocation. getAddrStr ());
  47. }
  48. If(PoiLocation. hasPoi ()){
  49. Sb. append ("\ NPoi :");
  50. Sb. append (poiLocation. getPoi ());
  51. }Else{
  52. Sb. append ("noPoi information ");
  53. }
  54. LogMsg (sb. toString ());
  55. }
  56. }
Set parameters

Positioning parameters include: positioning mode (single positioning, timed positioning), return coordinate type, and whether to enable GPS. Eg:

 
  1. LocationClientOption option =NewLocationClientOption ();
  2. Option. setOpenGps (True);
  3. Option. setAddrType ("detail ");
  4. Option. setCoorType ("gcj02 ");
  5. Option. setScanSpan (5000 );
  6. Option. disableCache (True);// Disable cache locating
  7. Option. setPoiNumber (5 );// Maximum number of POI returned
  8. Option. setPoiDistance (1000 );// Poi query distance
  9. Option. setPoiExtraInfo (True);// Whether POI's phone number, address, and other details are required
  10. 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.

 
  1. If(MLocClient! =Null& MLocClient. isStarted ())
  2. MLocClient. requestLocation ();
  3. Else
  4. 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.

 
  1. If(MLocClient! =Null& MLocClient. isStarted ())
  2. 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.

 
  1. // Location reminder code
  2. M1_yer =NewNotifyLister ();
  3. 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)
  4. MLocationClient. registerpolicy (mationyer );
  5. // After registering the location reminder listening event, you can use setpolicylocation to modify the location reminder settings, which will take effect immediately after modification.

 

 
  1. // BDNotifyListner implementation
  2. Public ClassNotifyListerExtendsBDNotifyListener {
  3. Public VoidOnNotify (BDLocation mlocation,FloatDistance ){
  4. MVibrator01.vibrate (1000 );// The vibration reminder has reached the specified position
  5. }
  6. }

 

 
  1. // Cancel the location reminder
  2. MLocationClient. removeNotifyEvent (mNotifyer );

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.