Use:
View the existing POI information in the gold map and escalate the GPS location error.
Preparatory work:
is to apply for Development Account in the gold, build a cloud. (Process slightly.) )
_name field as a unique indicator.
The Cloud Chart table adds a field reportid that identifies which endpoint is reporting the location information and prevents duplicate escalation.
Main code:
1. Turn on GPS positioning:
private void Initgps () {
Mylocation.settext ("Start positioning ...");
Locationmanager = Locationmanagerproxy.getinstance (poiactivity.this);
API positioning using GPS positioning method, the first parameter is positioning provider, the second parameter time is 2000 milliseconds, the third parameter distance interval unit is meters, the fourth parameter is to locate the listener
Locationmanager.requestlocationupdates (
Locationmanagerproxy.gps_provider, (a);
Locationmanager.requestlocationdata (Locationmanagerproxy.gps_provider,
(a);
}
2. Turn off GPS positioning:
private void Stopgps () {
Mylocation.settext ("positioning Stop");
Locationisok = false;
if (Locationmanager! = null) {
Locationmanager.removeupdates (this);
}
Locationmanager = null;
}
3. Get current GPS information
/**
* GPS Positioning callback method
*/
@Override
public void onlocationchanged (amaplocation location) {
if (location! = null) {
Double Geolat = Location.getlatitude ();
Double geolng = Location.getlongitude ();
This.lat = Geolat;
THIS.LNG = GEOLNG;
String str = ("Positioned successfully: (" + geolng + "," + Geolat + ")"
+ "\ n accuracy:" + location.getaccuracy () + "M"
+ "\ n Positioning mode:" + location.getprovider () + "\ n Positioning time:" + amaputil
. Converttotime (Location.gettime ()));
Mylocation.settext (str);
Thisplocation = geolng + "," + Geolat;
Locationisok = true;
} else {
Locationisok = false;
}
}
4. Get the phone string number as ReportID
Private String Getimei () {
String is = null;
try {
Telephonymanager Telephonymanager = (telephonymanager) CX
. Getsystemservice (Context.telephony_service);
is = Telephonymanager.getdeviceid ();
} catch (Exception e) {
is = "";
}
return is;
}
5. Query for duplicate HttpGet method
public int checkexist (string sname, String srid) {
//String Sname poi name, string Srid phone imei
string BASEURL = "http://yuntuapi.amap.com/datamanage/data/list?key= your key"
+ "limit=10&page=1&filter=" ;
String lasturl = "&tableid= your TableID";
String asks = "";
//Check if the IMEI is empty
Srid.replaceall ("", "");
if (Srid = = NULL | | srid.length () <= 0) {
asks = "_name:" + sname;
} else {
asks = "_name:" + sname + "+reportid:" + Srid;
}
;
String Countid = "10";//failed to obtain information, return >1 value for judgment.
try {
//Create a HttpClient object
HttpClient HttpClient = new Defaulthttpclient ();
HttpGet request = new HttpGet (BASEURL + asks + Lasturl);
Request.addheader ("Accept", "Text/json");
//JSON parsing process
HttpResponse response = Httpclient.execute (request);
Get httpentity
Httpentity entity = response.getentity ();
int code = Response.getstatusline (). Getstatuscode ();
if (code = =) {
//Gets the result information of the response
String JSON = entityutils.tostring (entity, "UTF-8");
The parsing process for JSON
if (JSON! = null) {
Jsonobject jsonobject = new Jsonobject (JSON);
Countid = Jsonobject.get ("Count"). ToString ();
Testcount = Countid;
TestInfo = jsonobject.get ("info"). toString ();
}
}
} catch (Clientprotocolexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (Jsonexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Return integer.valueof (Countid). Intvalue ();
}
6. HttpPost method for reporting GPS location information
Private Boolean Postreport (String sname, String sid, String slocation) {
String BASEURL = "Http://yuntuapi.amap.com/datamanage/data/create?";
String KEYS = "Your Key";
String TID = "Your TableID";
try {
HttpClient HttpClient = new Defaulthttpclient ();
String uri = baseurl;//
HttpPost HttpPost = new HttpPost (URI);
Httppost.addheader ("Content-type",
"application/x-www-form-urluncoded");
Jsonobject obj = new Jsonobject ();
Obj.put ("_name", sname);
Obj.put ("_location", slocation);
Obj.put ("ReportID", sid);
list<namevaluepair> formparams = new arraylist<namevaluepair> ();
Formparams.add (New Basicnamevaluepair ("key", KEYS));
Formparams.add (New Basicnamevaluepair ("TableID", TID));
Formparams.add (New Basicnamevaluepair ("Data", obj.tostring ()));
Urlencodedformentity uefentity;
uefentity = new Urlencodedformentity (formparams, "UTF-8");
Httppost.setentity (uefentity);
HttpResponse response;
Response = Httpclient.execute (HttpPost);
Verify the status code if the data is successfully received
int code = Response.getstatusline (). Getstatuscode ();
if (code = = 200) {
String rev = entityutils.tostring (Response.getentity ());//Return JSON format:
obj = new Jsonobject (rev);
String infos = obj.getstring ("info");
String stats = obj.getstring ("status");
if (Infos.equals ("OK")) {
return true;
}
}
} catch (Clientprotocolexception e) {
} catch (IOException e) {
} catch (Exception e) {
}
return false;
}
7. Update the interface thread
Private class Asynpost extends Asynctask<void, Void, string> {
Private final String TAG = "Dopost";
The OnPreExecute method executes after execute ()
@Override
protected void OnPreExecute () {
LOG.I (TAG, "OnPreExecute () enter");
Postisok = false;
}
The Oncancelled method is used to cancel task execution and update the UI
@Override
protected void oncancelled () {
LOG.I (TAG, "oncancelled () called");
Postisok = false;
}
@Override
protected void OnPostExecute (String result) {
Mpoitextview.settext (result);
Dissmissprogressdialog ();
if (Result.equals ("true")) {
Toast.maketext (CX, "Your information has been successfully submitted", Toast.length_long)
. Show ();
}else if (Result.equals ("false")) {
Toast.maketext (CX, "Your information submission failed because you have submitted the information. ",
Toast.length_long). Show ();
}else if (result.equals ("error")) {
Toast.maketext (CX, "your message submission failed because it could be a network problem,"
Toast.length_long). Show ();
};
}
@Override
Protected String doinbackground (Void ... arg0) {
TODO auto-generated Method Stub
String rr = "";
if (Checkexist (thispname, repid) = = 0) {
if (Postreport (Thispname, Repid, thisplocation)) {
Postisok = true;
rr = "true";
}else{
Postisok = false;
rr = "error";
};
} else {
Postisok = false;
rr = "false";
}
return RR;
}
}
Gold Map App--Interact with cloud background