Journeywork State Technical Department-Chen Wenshu
Baidu map of Baidu Eagle Eye trajectory use
Take the takeaway delivery for example, the distribution staff from the order to upload the driving trajectory, the user can view the travel track of the distribution staff in real-time, for the point of sale of users can more directly observe their orders in the distribution process, no longer need to call the merchant phone calls urged.
When thinking about solutions, the first thought was to constantly get the latitude and longitude of the distribution clerk in the background, upload it to the server, and then refresh the data in real-time on the user side to display on the map. This method, although the idea is very simple, but the data upload and refresh, but the accuracy of the trajectory and positioning precision has a great relationship, if the network environment is weak, such as the situation, positioning latitude and longitude data must be East Crooked West Twist, display on the map of the trajectory information is also disorder.
Backstage found Baidu map on-line Eagle Eye trajectory Service SDK, can achieve the tracking of an object, and displayed on the Baidu map, looks like we want to function, then we first to implement the function of this SDK.
First you have to create a new application in the API console, the application type is Android app, all the API is ticked, recently created new application added "release version SHA1", "Development Board SHA1", greatly facilitates the debugging state of the function development, And the issue that the SDK could not be initialized due to a change in signature SHA1 after the signature was launched.
Access to the development version of SHA1:
Can be viewed directly in Eclipse: Android---build with Windows---preferance
Get the release version of SHA1:
Using Keytool
1th step: Run into the console
2nd step: Navigate to the. Android folder, enter CD. Android
3rd Step: Enter Keytool-list-v-keystore Debug.keystore, you will get three kinds of fingerprint certificate, select the SHA1 type of certificate (key password is Android), for example:
Where Keytool is the JDK's own tool; Debug.keystore is the full path to the Android signed certificate file
After a successful creation, you can get the appropriate AK in the console, and in the project Androidmanifest.xml you need to configure the AK
Use Baidu positioning function of the students know how to use the
Next to Http://lbsyun.baidu.com/index.php?title=yingyan/manage.
Create Eagle Eye Service, get a service ID that initializes the Eagle Eye Service in your code
Import the development package into the project
If you use the other SDK of Baidu map, please go to http://lbsyun.baidu.com/index.php?title=androidsdk/sdkandev-download
Select the features that are useful to download in the project, remember not to download each SDK separately, otherwise it will cause the conflict between the package, this is the heart of Baidu pit tired.
Choose to download the Unified SDK after importing the relevant so file and jar, Eagle Eye SDK only need to import jar package and Armeabi on it, x86, x86_64 do not need to import, or Baidu will appear failure to initialize the problem. If you have an SDK that uses the Jpush Aurora push, you only need to import the jar package and Armeabi, otherwise it will also conflict. (I really feel that the compatibility of the SDK of Baidu is very urgent, it is easy to appear the conflict between library and library (/"≡_≡)/~┴┴)
You can then start the project configuration,
Home the first step to create an application-acquired AK configuration to Androidmanifest.xml
<application> <meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="AK key" /> <service android:name="com.baidu.trace.LBSTraceService" android:enabled="true" android:exported="true" ></service><service<!-- 我们自己的轨迹服务 --> android:name="com.mcandmc.xxxxxxx.service.MyService" android:enabled="true" android:exported="false" android:process=":track" ></service></application>
Add various permissions
<!--this permission for network positioning--><uses-permission android:name= "Android.permission.ACCESS_COARSE_LOCATION"/> <! --this permission is used to access the GPS location--><uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION"/><!-- For accessing WiFi network information, WiFi information will be used for network location--><uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/> <!--get carrier information to support the interface--><uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE" that provides carrier-related information /><!--This permission to get access to WiFi, WiFi information will be used for network positioning--><uses-permission android:name= "android.permission.CHANGE_ Wifi_state "/><!--allow programs to connect to paired Bluetooth devices--><uses-permission android:name=" Android.permission.BLUETOOTH "/ ><!--allows programs to discover and pair Bluetooth devices--><uses-permission android:name= "Android.permission.BLUETOOTH_ADMIN"/><!-- Used to read the current state of the phone--><uses-permission android:name= "Android.permission.READ_PHONE_STATE"/><!--write to the extended storage, Write data to the expansion card for writing offline location data--><uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/><!--access network, network location requires internet--><uses-permission android:name= "Android.permission.INTERNET"/><!--SD card Read access, User writes offline location data--><uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/><!-- Allows an app to read low-level system log files--><uses-permission android:name= "Android.permission.READ_LOGS"/><!--allow access to vibrating devices-- <uses-permission android:name= "Android.permission.VIBRATE"/><!--screen remains awake without lock screen--><uses-permission Android:name= "Android.permission.WAKE_LOCK"/><!--allow applications to change settings and shortcuts in the home screen--><uses-permission android: Name= "Android.permission.WRITE_SETTINGS"/>
Core code
public class MyService extends service{private static final String TAG = "MyService"; Trace service protected static trace trace = null; Eagle Eye Service ID, the service ID of the developer created by the Eagle Eye Service public static final long serviceId = xxxxxxxx; Trace Service Type//0: does not establish the socket long connection,//1: establishes the socket long connection but does not upload the location data,//2: establishes the socket long connection and uploads the location data) Private int tracetype = 2; Trace service client public static lbstraceclient client = NULL; Entity listener public static onentitylistener entitylistener = null; Turn on trace Service listener protected onstarttracelistener starttracelistener = null; Stop track service listener protected static Onstoptracelistener Stoptracelistener = null; Acquisition period (units: seconds) Private int gatherinterval = 10; Set packing period (in seconds) private int packinterval = 20; protected static Boolean Istracestart = false; The phone IMEI number is set to a unique track marker number that, as long as the value is unique, can be used as the identification number of the track, and using the same identity will cause the trajectory to clutter the private String imei;public ibinder onbind (Intent arg0) { return null; } @Override public int onstartcommand (Intent Intent, int flags, int startid) {if (intent! = null && Intent.getextras ()! = null) {imei= INTENT.GETST Ringextra ("IMEI"); } init (); Return Super.onstartcommand (Intent, Start_sticky, Startid); }//was destroyed when the anti-registration broadcast receiver public void OnDestroy () {Super.ondestroy (); Stoptrace (); }/** * Initialize */private void init () {//Initialize trace service client = new Lbstraceclient (this); Set the positioning mode Client.setlocationmode (locationmode.high_accuracy); init trace Service trace = new Trace (this, serviceId, IMEI, tracetype);//acquisition period, upload period client.setinterval (Gatherinterva L, Packinterval); Set the HTTP request protocol type 0:http,1:https Client.setprotocoltype (0); Initialize listener Initlistener (); Launch track upload Starttrace (); }//Open trace service private void Starttrace () {//Open Trace service Client.starttrace (Trace, Starttraceli Stener); }//Stop track service public static void StopTRace () {//Stop trace service via the Trace service client logutil.i (TAG, "stoptrace (), Istracestart:" + Istracestart); if (Istracestart) {Client.stoptrace (trace, Stoptracelistener); }}//Initialize listener private void Initlistener () {Initonentitylistener (); Initialize the open Track Service Listener Initonstarttracelistener (); Initialize Stop track Service listener Initonstoptracelistener ();} /** * Initialize Onstarttracelistener */private void Initonstarttracelistener () {//Initialize Starttracelistener Starttracelistener = new Onstarttracelistener () {//Open Trace Service Callback interface (ARG0: Message encoding, ARG1: message content, Details view class reference) Public V OID ontracecallback (int arg0, String arg1) {logutil.i (TAG, "Turn on trace callback interface [message code:" + arg0 + ", message content:" + arg1 + "]"); if (0 = = Arg0 | | 10006 = = arg0) {Istracestart = true; }}//Track service Push interface (for receiving server-side push messages, arg0: Message type, ARG1: message content, Details view class reference) public void Ontracepushcallback (byte arg 0, String arg1) {logutil.i (TAG, "Trace Service push interface message [Message type:" + arg0 + ", message content:" + arg1 + "]"); } }; }//Initialize Onstoptracelistener private void Initonstoptracelistener () {stoptracelistener = new Onstoptraceliste NER () {///Track service stopped successfully public void onstoptracesuccess () {logutil.i (TAG, "stop trajectory Service succeeded"); Istracestart = false; Stopself (); }//Track service Stop failed (arg0: Error encoding, ARG1: message content, Details view class reference) public void onstoptracefailed (int arg0, String arg1) {logutil.i (TAG, "Stop track Service interface message [ERROR code:" + arg0 + ", message content:" + arg1 + "]"); } }; }//Initialize Onentitylistener private void Initonentitylistener () {entitylistener = new Onentitylistener () { Request failed callback interface @Override public void Onrequestfailedcallback (String arg0) {method stub Looper.prepare (); Logutil.i (TAG, "Entity request failed callback interface message:" + arg0); Toast.maketext (getApplicationContext (), "Entity request failed callback interface message:" + arg0, Toast.length_short). Show (); Looper.loop (); }//Add entity callback interface @Override public void Onaddentitycallback (String arg0) { Looper.prepare (); Logutil.i (TAG, "Add entity Callback Interface message:" + arg0); Toast.maketext (Getapplicationcontext (), "Add Entity Callback Interface message:" + arg0, Toast.length_short). Show (); Looper.loop (); }//Query the Entity list callback interface @Override public void Onqueryentitylistcallback (String message) { Logutil.i (TAG, "Onqueryentitylistcallback:" + message); } @Override public void Onreceivelocation (tracelocation) {}};}
Start Servise in activity
Intent intent = new Intent();intent.putExtra("imei", "xxxxxx");intent.setClass(this, MyService.class);startService(intent);
So the app will silently upload tracks in the background.
If you want to see if the track is uploaded successfully, you can view it in the track management desk, as to how to implement the tracking of the distribution staff in the app will be mentioned in the following article.
Android Baidu Eagle Eye Trajectory