/** *Initialize positioning parameter configuration */ private void initLocationOption ( ) { //Locate the client of the service. The host program declares this class on the client and calls it. Currently, it only supports starting in the main thread LocationClient locationClient = new LocationClient ( getApplicationContext ( ) ) ; //Declare LocationClient class instance and configure location parameters LocationClientOption locationOption = new LocationClientOption ( ) ; MyLocationListener myLocationListener = new MyLocationListener ( ) ; //Register listener function locationClient . registerLocationListener ( myLocationListener ) ; //Optional, default high-precision, set positioning mode, high-precision, low-power, device only locationOption . setLocationMode ( LocationMode . Hight_Accuracy ) ; //Optional, default gcj02, set the returned positioning result coordinate system. If it is used with Baidu Maps, it is recommended to set it to bd09ll; locationOption . setCoorType ( "gcj02" ) ; //Optional, the default is 0, that is, the location is only made once. It is valid to set the interval between successive location requests to be greater than or equal to 1000ms locationOption . setScanSpan ( one thousand ) ; //Optional. Set whether address information is required. It is not required by default locationOption . setIsNeedAddress ( true ) ; //Optional, set whether address description is required locationOption . setIsNeedLocationDescribe ( true ) ; //Optional, set whether the equipment direction result is required locationOption . setNeedDeviceDirect ( false ) ; //Optional, default false, set whether to output satellite positioning results at 1S1 frequency when satellite positioning is valid locationOption . setLocationNotify ( true ) ; //Optional. The default is true. Locate the SDK as a SERVICE and place it in an independent process. Set whether to kill the process at the time of stop. The default is not to kill the process locationOption . setIgnoreKillProcess ( true ) ; //Optional, false by default, set whether to need location semantic results, which can be obtained in BDLocation.getLocationDescription, and the results are similar to "near Tiananmen Square in Beijing" locationOption . setIsNeedLocationDescribe ( true ) ; //Optional, false by default, set whether POI results are required, which can be obtained in BDLocation.getPoiList locationOption . setIsNeedLocationPoiList ( true ) ; //Optional, default false, set whether to collect CRASH information, default collection locationOption . SetIgnoreCacheException ( false ) ; //Optional, default false, set whether to enable satellite positioning locationOption . setOpenGnss ( true ) ; //Optional. The default is false. It is used to set whether altitude information is required during positioning. It is not required by default. Except for the basic positioning version, it is available locationOption . setIsNeedAltitude ( false ) ; //Set to turn on the automatic location callback mode. When this switch is turned on, as long as the location SDK detects a location change, it will actively call back to the developer. In this mode, the developer no longer needs to care about the location interval, but the location SDK itself will timely call back to the developer when it finds a location change locationOption . setOpenAutoNotifyMode ( ) ; //Set to turn on the automatic callback position mode. When this switch is turned on, as long as the positioning SDK detects a position change, it will actively call back to the developer locationOption . setOpenAutoNotifyMode ( three thousand , one , LocationClientOption . LOC_SENSITIVITY_HIGHT ) ; //The configured LocationClientOption object needs to be passed to the LocationClient object through the setLocOption method for use locationClient . setLocOption ( locationOption ) ; //Start positioning locationClient . start ( ) ; } /** *Realize positioning callback */ public class MyLocationListener extends BDAbstractLocationListener { @ Override public void onReceiveLocation ( BDLocation location ) { //BDLocation here is the location result information class. All the results related to location can be obtained through its various get methods //The following is only a partial list of results related to longitude and latitude (commonly used) //For more result information, please refer to the description in BDLocation class in the class reference //Get latitude information double latitude = location . getLatitude ( ) ; //Get longitude information double longitude = location . getLongitude ( ) ; //Get the positioning precision, the default value is 0.0f float radius = location . getRadius ( ) ; //Obtain the longitude and latitude coordinate type, which is subject to the coordinate type set in LocationClientOption String coorType = location . getCoorType ( ) ; //Get the location type and location error return code. For specific information, refer to the description in BDLocation class in the class reference int errorCode = location . getLocType ( ) ; } }
/** *Initialize positioning parameter configuration */ private void initLocationOption ( ) { //Locate the client of the service. The host program declares this class on the client and calls it. Currently, it only supports starting in the main thread LocationClient locationClient = new LocationClient ( getApplicationContext ( ) ) ; //Declare LocationClient class instance and configure location parameters LocationClientOption locationOption = new LocationClientOption ( ) ; MyLocationListener myLocationListener = new MyLocationListener ( ) ; //Register listener function locationClient . registerLocationListener ( myLocationListener ) ; //Optional, default high-precision, set positioning mode, high-precision, low-power, device only locationOption . setLocationMode ( LocationMode . Hight_Accuracy ) ; //Optional, default gcj02, set the returned positioning result coordinate system. If it is used with Baidu Maps, it is recommended to set it to bd09ll; locationOption . setCoorType ( "gcj02" ) ; //Optional, the default is 0, that is, the location is only made once. It is valid to set the interval between successive location requests to be greater than or equal to 1000ms locationOption . setScanSpan ( one thousand ) ; //Optional. Set whether address information is required. It is not required by default locationOption . setIsNeedAddress ( true ) ; //Optional, set whether address description is required locationOption . setIsNeedLocationDescribe ( true ) ; //Optional, set whether the equipment direction result is required locationOption . setNeedDeviceDirect ( false ) ; //Optional, default false, set whether to output Gnss results at 1S1 frequency when Gnss is valid locationOption . setLocationNotify ( true ) ; //Optional. The default is true. Locate the SDK as a SERVICE and place it in an independent process. Set whether to kill the process at the time of stop. The default is not to kill the process locationOption . setIgnoreKillProcess ( true ) ; //Optional, false by default, set whether to need location semantic results, which can be obtained in BDLocation.getLocationDescription, and the results are similar to "near Tiananmen Square in Beijing" locationOption . setIsNeedLocationDescribe ( true ) ; //Optional, false by default, set whether POI results are required, which can be obtained in BDLocation.getPoiList locationOption . setIsNeedLocationPoiList ( true ) ; //Optional, default false, set whether to collect CRASH information, default collection locationOption . SetIgnoreCacheException ( false ) ; //Optional, default false, set whether to enable satellite positioning locationOption . setOpenGnss ( true ) ; //Optional. The default is false. It is used to set whether altitude information is required during positioning. It is not required by default. Except for the basic positioning version, it is available locationOption . setIsNeedAltitude ( false ) ; //Set to turn on the automatic location callback mode. When this switch is turned on, as long as the location SDK detects a location change, it will actively call back to the developer. In this mode, the developer no longer needs to care about the location interval, but the location SDK itself will timely call back to the developer when it finds a location change locationOption . setOpenAutoNotifyMode ( ) ; //Set to turn on the automatic callback position mode. When this switch is turned on, as long as the positioning SDK detects a position change, it will actively call back to the developer locationOption . setOpenAutoNotifyMode ( three thousand , one , LocationClientOption . LOC_SENSITIVITY_HIGHT ) ; //The configured LocationClientOption object needs to be passed to the LocationClient object through the setLocOption method for use locationClient . setLocOption ( locationOption ) ; //Start positioning locationClient . start ( ) ; } /** *Realize positioning callback */ public class MyLocationListener extends BDAbstractLocationListener { @ Override public void onReceiveLocation ( BDLocation location ) { //BDLocation here is the location result information class. All the results related to location can be obtained through its various get methods //The following is only a partial list of results related to longitude and latitude (commonly used) //For more result information, please refer to the description in BDLocation class in the class reference //Get latitude information double latitude = location . getLatitude ( ) ; //Get longitude information double longitude = location . getLongitude ( ) ; //Get the positioning precision, the default value is 0.0f float radius = location . getRadius ( ) ; //Obtain the longitude and latitude coordinate type, which is subject to the coordinate type set in LocationClientOption String coorType = location . getCoorType ( ) ; //Get the location type and location error return code. For specific information, refer to the description in BDLocation class in the class reference int errorCode = location . getLocType ( ) ; } }
//The core implementation code is as follows. For details, please refer to the official website Demo. //Start the foreground positioning service: Notification . Builder builder = new Notification . Builder ( MainActivity . this . getApplicationContext ( ) ) ; //Get a Notification constructor Intent nfIntent = new Intent ( MainActivity . this . getApplicationContext ( ) , MainActivity . class ) ; builder . setContentIntent ( PendingIntent . getActivity ( MainActivity . this , zero , nfIntent , zero ) ) //Set PendingIntent . setContentTitle ( Background positioning in progress ) //Set the title in the drop-down list . setSmallIcon ( R . mipmap . ic_launcher ) //Set small icons in the status bar . setContentText ( "Background location notification" ) //Set context content . setAutoCancel ( true ) . setWhen ( System . currentTimeMillis ( ) ) ; //Set the time when this notification occurs Notification notification = null ; notification = builder . build ( ) ; notification . defaults = Notification . DEFAULT_SOUND ; //Set as default sound mLocClient . enableLocInForeground ( one thousand and one , notification ) ; //Turn up the foreground positioning //Stop the foreground location service: mLocClient . disableLocInForeground ( true ) ; //Close foreground positioning and remove the notice bar at the same time
public LocationClient mLocationClient = null ; public BDNotifyListener myListener = new MyNotifyListener ( ) ; /** *Please declare the LocationClient class object in the main thread. The initialization of this object needs to pass in the Context type parameter. It is recommended to use the getApplicationConext () method to obtain the valid Context of the whole process. */ public void onCreate ( ) { //Declare LocationClient class mLocationClient = new LocationClient ( getApplicationContext ( ) ) ; //Register listener function mLocationClient . registerNotify ( myListener ) ; } /** *Define the MyNotifyLister class, inherit BDNotifyListener, and implement the callback of location listening. */ public class MyNotifyLister extends BDNotifyListener { public void onNotify ( BDLocation mlocation , float distance ) { //Reached near the set listening position } } //Call the setNotifyLocation method of BDNotifyListener to set the location message reminder. //Set position reminder. The four parameters are latitude, precision, radius and coordinate type myListener . setNotifyLocation ( forty . 0f , one hundred and sixteen . 0f , three thousand , mLocationClient . getLocOption ( ) . getCoorType ( ) ) ; //When positioning is enabled, the SDK will automatically enable the monitoring of location message alerts mLocationClient . start ( ) ; //Call the removeNotifyEvent method of BDNotifyListener to cancel location listening mLocationClient . removeNotifyEvent ( myListener ) ;