diff options
| -rw-r--r-- | src/cgeo/geocaching/cgData.java | 56 |
1 files changed, 36 insertions, 20 deletions
diff --git a/src/cgeo/geocaching/cgData.java b/src/cgeo/geocaching/cgData.java index ff4cf56..19e3015 100644 --- a/src/cgeo/geocaching/cgData.java +++ b/src/cgeo/geocaching/cgData.java @@ -1407,18 +1407,40 @@ public class cgData { * * @param cursor * a Cursor representing a row in the database - * @return the coordinates, or null if latitude or longitude is null + * @param indexLat + * index of the latitude column + * @param indexLon + * index of the longitude column + * @return the coordinates, or null if latitude or longitude is null or the coordinates are invalid */ - private static Geopoint getCoords(final Cursor cursor) { - final int indexLat = cursor.getColumnIndex("latitude"); - final int indexLon = cursor.getColumnIndex("longitude"); + private static Geopoint getCoords(final Cursor cursor, final int indexLat, final int indexLon) { if (cursor.isNull(indexLat) || cursor.isNull(indexLon)) { return null; - } else { + } + + try { return new Geopoint(cursor.getDouble(indexLat), cursor.getDouble(indexLon)); + } catch (MalformedCoordinateException e) { + // TODO: check whether the exception should be returned to the caller instead, + // as it might want to remove an invalid geopoint from the database. + Log.e(cgSettings.tag, "cannot parse geopoint from database: " + e.getMessage()); + return null; } } + /** + * Retrieve coordinates from a Cursor + * + * @param cursor + * a Cursor representing a row in the database + * @return the coordinates, or null if latitude or longitude is null or the coordinates are invalid + */ + private static Geopoint getCoords(final Cursor cursor) { + final int indexLat = cursor.getColumnIndex("latitude"); + final int indexLon = cursor.getColumnIndex("longitude"); + return getCoords(cursor, indexLat, indexLon); + } + public boolean saveOwnWaypoint(int id, String geocode, cgWaypoint waypoint) { init(); @@ -2220,21 +2242,16 @@ public class cgData { int indexLongitude = cursor.getColumnIndex("longitude"); do { - cgDestination dest = new cgDestination(); - - try { - dest.setId((long) cursor.getLong(indexId)); - dest.setDate((long) cursor.getLong(indexDate)); - dest.setCoords(new Geopoint((double) cursor.getDouble(indexLatitude), - (double) cursor.getDouble(indexLongitude))); - + final cgDestination dest = new cgDestination(); + dest.setId((long) cursor.getLong(indexId)); + dest.setDate((long) cursor.getLong(indexDate)); + dest.setCoords(getCoords(cursor, indexLatitude, indexLongitude)); + + // If coordinates are non-existent or invalid, do not consider + // this point. + if (dest.getCoords() != null) { destinations.add(dest); - } catch (MalformedCoordinateException e) { - //If there is something wrong with coordinates in database - //we don't add - //TODO Might need to remove it from DB } - } while (cursor.moveToNext()); } @@ -3118,8 +3135,7 @@ public class cgData { list.id = ((int) cursor.getInt(indexId)) + 10; list.title = (String) cursor.getString(indexTitle); list.updated = (Long) cursor.getLong(indexUpdated); - list.coords = new Geopoint(cursor.getDouble(indexLatitude), - cursor.getDouble(indexLongitude)); + list.coords = getCoords(cursor, indexLatitude, indexLongitude); result.add(list); } while (cursor.moveToNext()); |
