Baidu Map API version v_3_1, Baidu positioning API version v_4.2
Package com.cmyy.ui;
Import org.json.JSONException;
Import Org.json.JSONObject;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.IntentFilter;
Import android.content.SharedPreferences;
Import Android.os.Bundle;
Import Android.util.Log;
Import com.baidu.location.BDLocation;
Import Com.baidu.location.BDLocationListener;
Import com.baidu.location.LocationClient;
Import com.baidu.location.LocationClientOption;
Import Com.baidu.mapapi.SDKInitializer;
Import Com.baidu.mapapi.map.BaiduMap;
Import Com.baidu.mapapi.map.MapStatusUpdate;
Import Com.baidu.mapapi.map.MapStatusUpdateFactory;
Import Com.baidu.mapapi.map.MapView;
Import Com.baidu.mapapi.map.MyLocationData;
Import Com.baidu.mapapi.model.LatLng;
Import COM.CMYY.R;
Import Com.cmyy.push.OnlineService;
Import Com.cmyy.push.Params;
Import Com.google.gson.Gson;
Import Com.google.gson.reflect.TypeToken; Real-time Logistics page public class Logisticstrackingactivity extends Baseactivity{private static final String Ltag = LogisticsTrackingActivity.class.getSimpleName ();
Private Mapview Mmapview;
Private Baidumap Mbaidumap;
Private Sdkreceiver Mreceiver;
Locating relevant private locationclient mlocclient; Public Mylocationlistenner MyListener = new Mylocationlistenner ();
Locate Boolean isfirstloc = true;//if the private Pushlocationreceiver Mpushlocationreceice is first positioned;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Register SDK Broadcast Listener Intentfilter iFilter = new Intentfilter ();
Ifilter.addaction (Sdkinitializer.sdk_broadtcast_action_string_permission_check_error);
Ifilter.addaction (Sdkinitializer.sdk_broadcast_action_string_network_error);
Mreceiver = new Sdkreceiver ();
Registerreceiver (Mreceiver, IFilter);
Setcontentview (r.layout.activity_tracking);
Notify the server of the need for logistic geolocation//register to receive location information broadcast Intentfilter Ipushfilter = new Intentfilter (); Ipushfilter.addaction ("Com.cmyy.ui.LogisticsTrackingAcTivity.
Pushlocationreceiver ");
Mpushlocationreceice = new Pushlocationreceiver ();
Registerreceiver (Mpushlocationreceice, Ipushfilter);
Start the service to receive Location Saveaccountinfo ();
Intent startsrv = new Intent (this, onlineservice.class);
Startsrv.putextra ("CMD", "RESET");
StartService (STARTSRV);
Show Map Mmapview = (mapview) Findviewbyid (R.id.bmapview);
Mbaidumap = Mmapview.getmap ();
Turn on the positioning layer mbaidumap.setmylocationenabled (true);
Positioning initialization/*mlocclient = new Locationclient (this);
Mlocclient.registerlocationlistener (MyListener);
locationclientoption option = new Locationclientoption (); Option.setopengps (TRUE);//Open GPS Option.setcoortype ("Bd09ll");
Set the coordinate type Option.setscanspan (1000);
mlocclient.setlocoption (option); Mlocclient.start (); */} public class Pushlocationreceiver extends Broadcastreceiver {public pushlocationreceiver () {} @Override public void OnReceive (context context, Intent Intent) {//received the location information sent by the distribution officer String me SsaGE = Intent.getstringextra ("Data");
LOG.I ("Receive", message);
Gson Gson = new Gson ();
Mylocationdata locdata = Gson.fromjson (message,new typetoken<mylocationdata> () {}.getType ());
Jsonobject Jsonobj;
Double latitude = 39.915;
Double longitude = 116.404;
try {jsonobj = new jsonobject (message);
Latitude = jsonobj.getdouble ("latitude");
Longitude= jsonobj.getdouble ("longitude");
} catch (Jsonexception e) {//TODO auto-generated catch block E.printstacktrace ();
} log.i ("Receive", String.valueof (latitude));
LOG.I ("Receive", String.valueof (longitude));
Mbaidumap.setmylocationdata (Locdata);
if (isfirstloc) {isfirstloc = false;
LATLNG ll = new Latlng (latitude, longitude);
Mapstatusupdate U = mapstatusupdatefactory.newlatlng (ll);
Mbaidumap.animatemapstatus (U); }}}/** * Locate SDK Listener function */public class Mylocationlistenner implements Bdlocationlistener {@Override Publi c void ONreceivelocation (bdlocation location) {//Map view is not processed after the newly received position is not handled if (site = = NULL | | mmapview = = NULL) retu
Rn Mylocationdata locdata = new Mylocationdata.builder (). Accuracy (Location.getradius ())//Here Set the direction information the developer gets, clockwise 0-36
0. Direction (+). Latitude (Location.getlatitude ()). Longitude (Location.getlongitude ()). build ();
Mbaidumap.setmylocationdata (Locdata);
if (isfirstloc) {isfirstloc = false;
LATLNG ll = new Latlng (Location.getlatitude (), Location.getlongitude ());
Mapstatusupdate U = mapstatusupdatefactory.newlatlng (ll);
Mbaidumap.animatemapstatus (U); }} public void Onreceivepoi (Bdlocation poilocation) {}}/** * Constructs broadcast listener class, monitors SDK key authentication and network exception broadcast */public C Lass Sdkreceiver extends Broadcastreceiver {public void OnReceive (context context, Intent Intent) {String s = inten
T.getaction ();
LOG.D (Ltag, "action:" + s); if (S.equals (Sdkinitializer.sdk_broadtcast_action_string_permission_check_error){LOG.E ("logisticstrackingactivity", "Key validation error!), check the key settings in the Androidmanifest.xml file"); } else if (S.equals (Sdkinitializer.sdk_broadcast_action_string_network_error)) {LOG.E ("logisticstrackingactivity",
"Network error"); }}} protected void Saveaccountinfo () {sharedpreferences account = this.getsharedpreferences (params.default_pre_n
Ame,context.mode_private);
Sharedpreferences.editor Editor = Account.edit ();
Editor.putstring (Params.server_ip, "192.168.88.130");
Editor.putstring (Params.server_port, "9966");
Editor.putstring (Params.push_port, "9998");
Editor.putstring (Params.user_name, "333");
Editor.putstring (params.sent_pkgs, "0");
Editor.putstring (params.receive_pkgs, "0");
Editor.commit ();
} @Override protected void OnPause () {super.onpause ();
Pause the Map Control Mmapview.onpause () while the activity is paused;
} @Override protected void Onresume () {super.onresume ();
Restore the map control at the same time when activity resumes Mmapview.onresume ();
} @Override protected void OnDestroy () { Mlocclient.stop ();
Turn off positioning layer mbaidumap.setmylocationenabled (false);
Super.ondestroy ();
Cancel the Listener SDK broadcast unregisterreceiver (mreceiver);
Destroy the map control at the same time when the activity is destroyed Mmapview.ondestroy (); }}<strong> </strong>
5.2 Distribution Agent app sends geo-location information to Tracking server
Accept Server Push information saveaccountinfo ();
Set the server IP port Intent startsrv = new Intent (this, onlineservice.class);
StartService (STARTSRV);
push = new push (this);
Start geo-location monitoring Mbaidumapcontrol = new Baidumapcontrol (); Mbaidumapcontrol.location (Getapplicationcontext (), new Bdlocationlistener () {@Override public void Onreceiveloc ation (bdlocation location) {Mylocationdata Locdata = new Mylocationdata.builder (). Accuracy (Location.getradius ())///Here Set the direction information the developer obtains, clockwise 0-360. Direction (+). Latitude (Location.getlatitude ()). Longitude (location.getlongit
Ude ()). build ();
Gson Gson = new Gson ();
geolocation information String strlocation = Gson.tojson (locdata). toString ();
LOG.I ("Baidulocationapidem", strlocation);
Receive location/*stringbuffer sb = new StringBuffer (256);
Sb.append ("Time:"); Sb.append (Location.gettime ());
Positioning time Sb.append ("\nerror Code:"); Sb.append (Location.getloctypE ());
Sb.append ("\nlatitude:"); Sb.append (Location.getlatitude ());
The developer obtains the direction information, clockwise 0-360 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 ());
Sb.append ("\ndirection:");
Sb.append ("\naddr:");
Sb.append (Location.getaddrstr ());
Sb.append (Location.getdirection ());
} else if (location.getloctype () = = bdlocation.typenetworklocation) {sb.append ("\naddr:");
Sb.append (Location.getaddrstr ());
Carrier information Sb.append ("\noperationers:");
Sb.append (Location.getoperators ()); } log.i ("Baidulocationapidem", sb.tostring ());
*///location successfully sent to server push.send0x20 ("333", strlocation); }
});
not finished ....