我正在开发一个应用程序来跟踪车辆并计算他们行驶的距离。我在车上安装了一个设备,通过短信向我的手机发送LatLngs
。我的应用程序在SQLiteDatabase
中存储包含LatLng
的SMS。我的表格每行有一个纬度和经度。我的桌子上的行数日益增长。 当我从表中检索这些数据来绘制地图和计算距离时,我尝试从表中访问数据时,我的应用程序冻结了仅仅一秒钟。如何让它更快?任何建议请。在MainActivity中执行数据库操作是否会冻结UI?
public void calculateDistance(String date) {
googleMap.clear();
double cumulativeDistance = 0;
String selectLog1="select rowid _id,* from logTable where date='"+date+"' and deviceNo='"+deviceNo+"'";
Cursor cursor4=db.rawQuery(selectLog1,null);
if(cursor4.getCount()>2) {
while (cursor4.moveToNext()) {
String strLatLng = cursor4.getString(cursor4.getColumnIndex("log"));
strDate11=cursor4.getString(cursor4.getColumnIndex("date"));
String[] strArray = strLatLng.split(",");
latitude2 = Double.parseDouble(strArray[0]);
longitude2 = Double.parseDouble(strArray[1]);
time11=strArray[2];
coordList1.add(new LatLng(latitude2, longitude2));
timeList.add(strLatLng+","+time11);
}
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.ledorange);
for (String strData:timeList){
String[] strDatas=strData.split(",");
double latData=Double.parseDouble(strDatas[0]);
double lngData=Double.parseDouble(strDatas[1]);
String timeAt=strDatas[2];
googleMap.addMarker(new MarkerOptions().position(new LatLng(latData,lngData)).title(vehicleName).icon(bitmapDescriptor).snippet("Known @ " + timeAt + "," + strDate11));
}
timeList.removeAll(timeList);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(latitude2, longitude2), 12.5f);
googleMap.animateCamera(cameraUpdate);
googleMap.addPolyline(new PolylineOptions().addAll(coordList1).width(10).color(getResources().getColor(R.color.myBlue)));
DecimalFormat format = null;
for (int i = 0; i < (coordList1.size() - 1); i++) {
LatLng l1 = coordList1.get(i);
double lat1 = l1.latitude;
double lng1 = l1.longitude;
Location location1 = new Location("");
location1.setLatitude(lat1);
location1.setLongitude(lng1);
LatLng l2 = coordList1.get(i + 1);
double lat2 = l2.latitude;
double lng2 = l2.longitude;
Location location2 = new Location("");
location2.setLatitude(lat2);
location2.setLongitude(lng2);
format = new DecimalFormat("#.##");
double distance = location1.distanceTo(location2)/1000;
cumulativeDistance = cumulativeDistance + distance;
}
if (preferences.contains("errorRatio")) {
cumulativeDistance =cumulativeDistance + (cumulativeDistance/100) * preferences.getInt("errorRatio", 0);
}
tvDistance.setText(format.format(cumulativeDistance) + " Km's");
coordList1.removeAll(coordList1);
}else {
tvDistance.setText("No Data");
}
}
'索引'您的搜索和加入栏是你的朋友。而且'AsyncTask'也是。 –