Android Baidu map API usage tutorial, androidapi

Source: Internet
Author: User

Android Baidu map API usage tutorial, androidapi
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. <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"
  2. android:permission="android.permission.BAIDU_LOCATION_SERVICE">
  3. <intent-filter>
  4. <action android:name="com.baidu.location.service_v2.4"></action>
  5. </intent-filter>
  6. </service>

Declare permission

 
 
  1. <permission android:name="android.permission.BAIDU_LOCATION_SERVICE"></permission>
  2. <uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE"></uses-permission>
  3. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
  4. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
  5. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
  6. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
  7. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
  8. <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
  9. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
  10. <uses-permission android:name="android.permission.INTERNET" />
  11. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
  12. <uses-permission android:name="android.permission.READ_LOGS"></uses-permission>
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 to 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 class MyLocationListenner implements BDLocationListener {
  2. @Override
  3. public void onReceiveLocation(BDLocation location) {
  4. if (location == null)
  5. return ;
  6. StringBuffer sb = new StringBuffer(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.getLongitude());
  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 void onReceivePoi(BDLocation poiLocation) {
  30. if (poiLocation == null){
  31. return ;
  32. }
  33. StringBuffer sb = new StringBuffer(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.getLongitude());
  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.