高德地圖應用——與雲圖後臺交互

用途:git

在高德地圖中查看已存在的興趣點信息,並上報GPS位置錯誤。json

準備工做:
是在高德申請開發帳號,創建一個雲圖。(過程略。)
_name字段做爲惟一標示。
雲圖的表增長一個字段reportid,用以辨別是哪一個終端上報的位置信息,防止重複上報。api


主要代碼:
1.開啓GPS定位:網絡

private void initgps() {
myLocation.setText("開始定位...");
locationManager = LocationManagerProxy.getInstance(PoiActivity.this);
// API定位採用GPS定位方式,第一個參數是定位provider,第二個參數時間最短是2000毫秒,第三個參數距離間隔單位是米,第四個參數是定位監聽者
// locationManager.requestLocationUpdates(
// LocationManagerProxy.GPS_PROVIDER, 2000, 10, this);
locationManager.requestLocationData(LocationManagerProxy.GPS_PROVIDER,
2000, 10, this);
}

 

2.關閉GPS定位:app

private void stopgps() {
myLocation.setText("定位中止");
locationisok = false;
if (locationManager != null) {
locationManager.removeUpdates(this);
}
locationManager = null;
}

 

3.獲取當前GPS信息ide

/**
* gps定位回調方法
*/
@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 = ("定位成功:(" + geoLng + "," + geoLat + ")"
+ "\n精 度 :" + location.getAccuracy() + "米"
+ "\n定位方式:" + location.getProvider() + "\n定位時間:" + AMapUtil
.convertToTime(location.getTime()));
myLocation.setText(str);
thisplocation = geoLng + "," + geoLat;
locationisok = true;
} else {
locationisok = false;
}
}

 

4.獲取手機串號做爲reportidpost

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.查詢是否重複HttpGet方法this

public int checkexist(String sname, String srid) {
// String sname 興趣點名稱, String srid 電話IMEI
String BASEURL = "http://yuntuapi.amap.com/datamanage/data/list?key=你的KEY"
+ "limit=10&page=1&filter=";
String LASTURL = "&tableid=你的tableid";
String asks = "";
// 檢查IMEI是否爲空
srid.replaceAll(" ", "");
if (srid == null || srid.length() <= 0) {
asks = "_name:" + sname;
} else {
asks = "_name:" + sname + "+reportid:" + srid;
}
;

String countid = "10";// 未成功獲取信息,返回>1的數值供判斷。
try {
// 建立一個HttpClient對象
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(BASEURL + asks + LASTURL);
request.addHeader("Accept", "text/json");
// JSON的解析過程
HttpResponse response = httpclient.execute(request);
// 獲取HttpEntity
HttpEntity entity = response.getEntity();
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
// 獲取響應的結果信息
String json = EntityUtils.toString(entity, "UTF-8");
// 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.上報GPS位置信息HttpPost方法
private boolean postreport(String sname, String sid, String slocation) {
String BASEURL = "http://yuntuapi.amap.com/datamanage/data/create?";
String KEYS = "你的KEY";
String TID = "你的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);
// 檢驗狀態碼,若是成功接收數據
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
String rev = EntityUtils.toString(response.getEntity());// 返回json格式:
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.更新界面線程url

private class Asynpost extends AsyncTask<Void, Void, String> {
private final String TAG = "dopost";

// onPreExecute方法在execute()後執行
@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute() enter");
postisok = false;
}

// onCancelled方法用於取消Task執行,更新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, "您的信息已成功提交", Toast.LENGTH_LONG)
.show();
}else if (result.equals("false")) {
Toast.makeText(cx, "您的信息提交失敗,緣由是:您已經提交過信息。",
Toast.LENGTH_LONG).show();
}else if (result.equals("error")){
Toast.makeText(cx, "您的信息提交失敗,緣由是:多是網絡問題,",
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;
}

}
相關文章
相關標籤/搜索