高德地图应用——与云图后台交互

用途:

在高德地图中查看已存在的兴趣点信息,并上报GPS位置错误。

准备工作:
是在高德申请开发账号,建立一个云图。(过程略。)
_name字段作为唯一标示。
云图的表增加一个字段reportid,用以辨别是哪个终端上报的位置信息,防止重复上报。


主要代码:
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定位:

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

 

3.获取当前GPS信息

/**
* 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.获取手机串号作为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.查询是否重复HttpGet方法

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.更新界面线程

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;
}

}

 

posted @ 2015-03-02 10:05  happyhills  阅读(1720)  评论(0编辑  收藏  举报