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.
- <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"
- android:permission="android.permission.BAIDU_LOCATION_SERVICE">
- <intent-filter>
- <action android:name="com.baidu.location.service_v2.4"></action>
- </intent-filter>
- </service>
Declare permission
- <permission android:name="android.permission.BAIDU_LOCATION_SERVICE"></permission>
- <uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE"></uses-permission>
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
- <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
- <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
- <uses-permission android:name="android.permission.READ_LOGS"></uses-permission>
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 to 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 class MyLocationListenner implements BDLocationListener {
- @Override
- public void onReceiveLocation(BDLocation location) {
- if (location == null)
- return ;
- StringBuffer sb = new StringBuffer(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.getLongitude());
- 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 void onReceivePoi(BDLocation poiLocation) {
- if (poiLocation == null){
- return ;
- }
- StringBuffer sb = new StringBuffer(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.getLongitude());
- 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 );