The front has realized the use of Baidu map to locate the function, positioning is just a basic support, now we can use it to do more things, such as location Alert service. In the module of Positioning API, Bdmap also provides a section of the geo-fencing service, but as it is described in the introduction, is only a beta version, Bo Master login Baidu lbs open Platform Forum, see the above mentioned bug still exist a lot, so here does not make this introduction. In the API, there is a location to remind the use of the introduction, this article mainly introduces this feature. It is important to note that in order to show the location of the alert function to achieve success, in the reminder service added a mobile phone vibration function.
First, the realization of location alert function:
public class Mainactivity<notifylister> extends Activity {
...
Public Notifylister Mnotifyer = null;//Add a positional reminder variable
...
protected void OnCreate (Bundle savedinstancestate) {
...
Initnotify ();//Use a function to locate the alert, you can clearly distinguish it, and define it below
...
}
Location Alert related code
private void Initnotify () {
Mnotifyer = new Notifylister ();//define Position reminder variable
Mnotifyer.setnotifylocation (latitude,longtitude,distance, "bd09ll");
4 parameters represent the coordinates of the point to which the position is to be alerted, in order: latitude, longitude, distance range, coordinate system type (GCJ02,GPS,BD09,BD09LL)
Mlocationclient.registernotify (Mnotifyer);
Register location alert listen to the event, you can change the location alert settings by Setnotifylocation, the change takes effect immediately
}
Bdnotifylistener: Position alert interface class for setting location reminders
public class Notifylister extends bdnotifylistener{
/**
* OnNotify (...): Location alert Listener Event specific implementation
* Mlocation indicates the current position,
* Distance is the distance value between the current coordinate center point and the coordinate point of the set position alert.
*/
public void OnNotify (bdlocation mlocation, float distance) {
Alert functions in the listening range
}
}
...
}
In this way, the basic Location Alert service has been completed, the next thing to do is to listen to the scope of the action to be done (here is the mobile phone vibration function).
Second, to achieve the alarm within the range of mobile phone vibration function:
First, add the permission to allow the phone to vibrate in the Androidmanifest.xml file:
<uses-permission android:name= "Android.permission.VIBRATE"/>
Next, you can edit the add vibration code directly in the main Java file:
public class Mainactivity<notifylister> extends Activity {
...
Private vibrator mvibrator;//Add variable to control the vibration of the phone
Private long[] Mtips = {1000,2000,1000,2000};//Add a long array to record the vibration frequency
...
protected void OnCreate (Bundle savedinstancestate) {
...
Mvibrator = (Vibrator) this.getsystemservice (Vibrator_service);
The vibration variable is defined as a vibrating service that can be provided by a mobile phone.
...
}
public class Notifylister extends bdnotifylistener{
public void OnNotify (bdlocation mlocation, float distance) {
Mvibrator.vibrate (Mtips,-1);
Increase the vibration of the phone within the range of reminders, the first parameter defines the vibration frequency, the second parameter defines the number of vibrations
}
}
The above has basically achieved the location of the service provided by Bdmap, but it is worth noting that the
Mnotifyer.setnotifylocation (latitude,longtitude,distance, "bd09ll");
Code inside the parameters, distance distance is in m units, but the front two is the latitude and longitude of Baidu coordinates as parameters, in the calculation of the time can not simply add and subtract, in the latitude and longitude and distance for the conversion can be intuitively calculated. Fortunately, in the API provided by Bdmap, there is also a way to achieve latitude and longitude and distance conversion:
latlng P1 = new latlng (x1, y1);//p1 store the latitude and longitude of the first point, where the x1,y1 is the latitude and longitude, the same
LATLNG P2 = new Latlng (x2, y2);//p2 store the latitude and longitude of the second point
Double distance = Distanceutil.getdistance (P1, p2);//Find the distance between two points, in units of M
Here, when applying distanceutil.getdistance (), be aware that you have downloaded the location-based computing tools SDK.
Baidu Map Open API first Experience (III)