所以我正在使用 Android points = new ArrayList<LatLng>();
每 3 秒跟踪一次用户的 GPS 位置,如果他们在一条足够长的路线上,有时可以达到 1,000 或更多点。在他们完成我的 php 并将其存储到 mysql 之后发送此路由的最佳方法是什么?我目前正在使用 volley 连接到我的数据库,但如果我尝试在一个循环中发送太多信息,它似乎会超时,IE 分别在每个位置。
这是我获取 LatLng 的方式:
mLastLocation = location;
LatLng latLng = new LatLng(latitude, longitude);
points.add(latlng);
这是我的截击请求:
RequestQueue queue = Volley.newRequestQueue(context);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_FOR_GPS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("GPS save route", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
//adding parameters to the request
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("command", "saveGPSroute");
params.put("userId", userId);
return params;
}
};
// Add the request to the RequestQueue.
queue.add(stringRequest);
最佳答案
我在 onLocationChanged() 上存储了与计数共享的偏好,并且每次我都在增加计数。
然后我创建了一种从共享偏好中收集 latlong 的方法,我正在制作 json 数组。使用POST方式发送数据不要使用GET方式发送大量的json数据。
@Override
public void onLocationChanged(Location location) {
editdata.putString("latitude" + data_count,
"" + location.getLatitude());
editdata.putString("longitude" + data_count,
"" + location.getLongitude());
editdata.putString("bearing" + data_count,
"" + location.getBearing());
editdata.putInt("count", data_count);
data_count++;
editdata.commit();
}
//collect data from preference like
ArrayList<HashMap<String, String>> arr_list = new ArrayList<HashMap<String, String>>();
JsonArray strJson ;
public void parse(){
arr_list .clear();
for (int i = first; i < tripdata.getInt("count", 0); i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("lat", "" + latitude);
map.put("long", "" + longitude);
map.put("bearing", bearing);
arr_list.add(map);
}
if (arr_list.size() > 0) {
strJson = new JSONArray(arr_list).toString();
strJson = "{\"latlng\":" + strJson + "}";
}
}
//here strJson is yout JSONArray Latlong data list
关于php - Android 将 Lat Long 发送到 php 并存储在 mysql 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47986547/