My application have to load some “points” from internal sqlite database of android, and it converts this points into lines on a map, then, from the database, I load other “points” which becomes markers on the map.
The problem is that the loading process described above is awfully slow. This process can take many seconds, and also minutes in the worst case.
The points in database come from a service, which trace your position. I have implemented in this service some methods to avoid a surplus of points, like time control (if two subsequent points have been taken too near in time, the second is not saved), position control (if two subsequent points have the same coordinates, the last will not be saved) and distance check (if two subsequent points are too near in distance, the last will not be saved).
Other computations can occour on these points during the loading, but slowness is present also in the best case, where it’s only the reading from database.
This is where I create the array which contains points for draw lines on map.
public BuildPointsList(Context context, long travelId) {
this.context=context;
this.travelId = travelId;
initializePointList();
}
private void initializePointList() {
pointsList = new ArrayList<Points>();
}
public List<Points> fromDB() {
TravelDB traveldb = new TravelDB(context);
traveldb.open();
Cursor cursor = traveldb.fetchAllGpsPoints(travelId);
loadPoints(cursor);
cursor.close();
traveldb.close();
return pointsList;
}
protected void loadPoints(Cursor cursor) {
initializePointList();
cursor.moveToFirst();
int gpsIndex = cursor.getColumnIndex(colGpsId);
int latitudeIndex = cursor.getColumnIndex(colGpsLatitude);
int longitudeIndex = cursor.getColumnIndex(colGpsLongitude);
int dateIndex = cursor.getColumnIndex(colGpsDate);
for (int progress=0; progress<cursor.getCount(); progress++) {
Points points = new Points();
points.setLatitude(cursor.getInt(latitudeIndex));
points.setLongitude(cursor.getInt(longitudeIndex));
points.setDataRilevamento(cursor.getLong(dateIndex));
points.setGpsId(cursor.getInt(gpsIndex));
pointsList.add(points);
cursor.moveToNext();
}
}
Here is where I build the array which contains points for markers, for every marker exist one or more images
private void buildPoints () {
List<Images> images = travel.getImages();
Log.i("DDV", "images:"+images.size());
// se esistono punti
if (!images.isEmpty()) {
length = images.size();
for (progress = 0 ; progress < length; progress++) {
currentImage = images.get(progress);
// verifico se una località con coordinate simili era già stata presa in esame
// in quel caso la raggruppo insieme alle altre
if ((oldImage != null) && (near(oldImage.getGeopoint(), currentImage.getGeopoint()))) {
overlayitem.addImage(currentImage);
if (currentImage.getAddress().length() == 0) {
if (oldImage.getAddress().length() > 0) {
currentImage.setAddress(oldImage.getAddress());
travel.addImages(currentImage);
}
}
}
// in questo caso vuol dire che le coordinate sono troppo differenti per unirle in una
// quindi salvo l'oggetto vecchio e ne instanzio uno nuovo
else if (oldImage != null) {
setTextAndSave(oldImage);
createOverlay(currentImage.getGeopoint());
oldImage = currentImage;
}
// in questo caso è la prima volta che instanzio una coordinata
else {
oldImage = currentImage;
// geocoding per determinare il nome della via dove è stato generato il contenuto
// se nel db non è presente
if (currentImage.getAddress().length() == 0)
currentImage = geoCode(currentImage);
// aggiungo il marker
createOverlay(currentImage.getGeopoint());
}
// faccio progredire la sbarra
onProgressUpdate((int)(progress*100/length));
}
setTextAndSave(oldImage);
}
}
This is where I build the line for the map
private void buildRoute() {
points = travel.getPoints();
length = points.size();
long tmp = DateManipulation.getCurrentTimeMs();
Log.i("DDV", "START PARSIN POINTSL: " +tmp);
if (length > 1) {
Points currentPoint = points.get(0);
Points oldPoint=currentPoint;
for (int progress=0; progress<length; progress++) {
currentPoint = points.get(progress);
if (currentPoint.getGeoPoint() != oldPoint.getGeoPoint()) {
oldPoint = currentPoint;
polyLine.add(currentPoint.getGeoPoint());
setZoomAndCenter(currentPoint);
}
onProgressUpdate((int)(progress*100/length));
}
}
saveCenterPosition();
}
If do you need something else, ask freely.
Thank you to all.
EDIT – Here is the method getGeoPoint
public GeoPoint getGeoPoint() {
GeoPoint geoPoint = new GeoPoint(latitude, longitude);
return geoPoint;
}
I would recommend you do some tracing with DDMS, it’ll show you where you’re spending your most time and help you optimize.
I suspect, after dealing with GeoPoint, that you’re not doing enough caching. The “new GeoPoint” constructor is extremely expensive so if you’re doing a few hundred operations of that instantiation, I recommend re-thinking how you cache that.
I’d like to see the logic behind
codePoint.getGeoPoint()I see that you’re storing the raw lat/lng in the database. That will be fast to go in and out. Again, I assume it’s the creation of your GeoPoint object that’s taking a lot of time. Sadly, there’s no way to make GeoPoint serializable and store that. So you’re stuck instantiating it as soon as you pull it out of the database.
Do a trace, let’s see what the result of that is. 🙂