diff --git a/app/src/main/java/de/apps4ics/mountainnavigation/MainActivity.java b/app/src/main/java/de/apps4ics/mountainnavigation/MainActivity.java index 4331466..3fa7002 100644 --- a/app/src/main/java/de/apps4ics/mountainnavigation/MainActivity.java +++ b/app/src/main/java/de/apps4ics/mountainnavigation/MainActivity.java @@ -275,8 +275,8 @@ @Override public void onClick(View v) { String key = res.getStringArray(R.array.hikeEntriesKeys)[position]; - if(key.equals(getString(R.string.hike_title_record_key))) { - if(hikeHandler.isRecording()) { + if (key.equals(getString(R.string.hike_title_record_key))) { + if (hikeHandler.isRecording()) { final ArrayList hikePoints = (ArrayList) hikeHandler.getPoints(); LayoutInflater inflater = getLayoutInflater(); @@ -297,11 +297,12 @@ @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { String key = getResources().getStringArray(R.array.hike_floorings_keys)[position]; - if(key.equals(getString(R.string.hike_flooring_street_key))) { - } else if(key.equals(getString(R.string.hike_flooring_grit_key))) { - } else if(key.equals(getString(R.string.hike_flooring_trail_key))){ + if (key.equals(getString(R.string.hike_flooring_street_key))) { + } else if (key.equals(getString(R.string.hike_flooring_grit_key))) { + } else if (key.equals(getString(R.string.hike_flooring_trail_key))) { } } + @Override public void onNothingSelected(AdapterView parent) { } @@ -321,14 +322,14 @@ @Override public void onClick(DialogInterface dialog, int which) { String name = input.getText().toString().trim(); - if(!name.equals("")) { + if (!name.equals("")) { hikeHandler.stopRecording(); view.setText(hikeEntries[position]); int floor = flooring.getSelectedItemPosition(); long time = System.currentTimeMillis(); long hikeId = poiHandler.addPoi(new Hike(0, hikeHandler.getLength(), hikeHandler.getTime(), (int) (time % 3), name, "You", time, hikeHandler.getUp(), hikeHandler.getDown(), floor)); - for(HikePoint hp : hikePoints) { + for (HikePoint hp : hikePoints) { poiHandler.addGp(new DbGeoPoint(hp.getLocation(), hp.getTimestamp(), hikeId, Types.HIKE)); } } @@ -360,11 +361,13 @@ @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } + @Override public void onTextChanged(CharSequence s, int start, int before, int count) { - if(s.length() > 0) saveHikeDialogButton.setEnabled(true); + if (s.length() > 0) saveHikeDialogButton.setEnabled(true); else saveHikeDialogButton.setEnabled(false); } + @Override public void afterTextChanged(Editable s) { } @@ -390,136 +393,146 @@ AlertDialog startHikeDialog = startHikeBuilder.create(); startHikeDialog.show(); } - } else if(key.equals(getString(R.string.hike_title_show_key))) { - //TODO Open dialog with all hikes around current position (or enter position) - ArrayList pois = (ArrayList) dbHandler.getPoiByType(Types.HIKE); - final List> points = new ArrayList<>(); - final ArrayList hikes = new ArrayList<>(); - for(Poi p : pois) { - Hike hike = (Hike) p; - points.add(dbHandler.getGeoPointsForPoi(hike.getId(), hike.getType())); - hikes.add(hike); - } - AlertDialog showHikesDialog = new AlertDialog.Builder(MainActivity.this) - .setAdapter(new HikeListAdapter(MainActivity.this, hikes, points), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - Polyline hikePath = new Polyline(MainActivity.this); - Hike hike = hikes.get(which); - List dbPoints = points.get(which); - List pathPoints = new ArrayList<>(); - float[] elevationDataPoints = new float[dbPoints.size()]; - String[] elevationDataPointsNames = new String[dbPoints.size()]; - final String[] elevationDataPointsTimes = new String[dbPoints.size()]; - double min = Double.MAX_VALUE; - double max = Double.MIN_VALUE; - long startTime = dbPoints.get(0).getTime(); - for(int i=0; i< dbPoints.size(); i++) { - DbGeoPoint dbGeoPoint = dbPoints.get(i); - elevationDataPointsTimes[i] = formatTimeIntervalToHms((int) (dbGeoPoint.getTime()-startTime)); - Log.d(TAG, "formatted: " + elevationDataPointsTimes[i]); - double alt = dbGeoPoint.getAlt(); - pathPoints.add(new GeoPoint(dbGeoPoint.getLat(), dbGeoPoint.getLon(), alt)); - //TODO REMOVE - alt = (int) (Math.random() * 100) + 40; - elevationDataPoints[i] = (int) alt; - elevationDataPointsNames[i] = (dbGeoPoint.getTime()-startTime)+"s"; - if(alt < min) min = alt; - if(alt > max) max = alt; + } else if (key.equals(getString(R.string.hike_title_show_key))) { + if (mLocation == null) { + Toaster("No location found!", true); //TODO dialog + } else { + final List hikes = poiHandler.getHikesAround(mLocation); + final List> points = new ArrayList<>(); + for (Hike hike : hikes) { + points.add(dbHandler.getGeoPointsForPoi(hike.getId(), hike.getType())); + } + AlertDialog.Builder showHikesDialogBuilder = new AlertDialog.Builder(MainActivity.this) + .setTitle(R.string.hike_list_title) + .setNegativeButton(R.string.close_button, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { } - LayoutInflater inflater = getLayoutInflater(); - View rowView = inflater.inflate(R.layout.hike_list_detail, null, true); - TextView titleView = (TextView) rowView.findViewById(R.id.hike_list_detail_title); - TextView authorView = (TextView) rowView.findViewById(R.id.hike_list_detail_author); - TextView lengthView = (TextView) rowView.findViewById(R.id.hike_list_detail_length); - TextView timeView = (TextView) rowView.findViewById(R.id.hike_list_detail_time); - TextView difficultyView = (TextView) rowView.findViewById(R.id.hike_list_detail_difficulty); - TextView upView = (TextView) rowView.findViewById(R.id.hike_list_detail_up); - TextView downView = (TextView) rowView.findViewById(R.id.hike_list_detail_down); - LineChartView elevationProfileNew = (LineChartView) rowView.findViewById(R.id.hike_list_detail_elevation_profile); - MapView hikeMap = (MapView) rowView.findViewById(R.id.hike_list_detail_map); - titleView.setText(hike.getName()); - authorView.setText(String.format(Locale.getDefault(), res.getString(R.string.hike_list_author), hike.getAuthor())); - lengthView.setText(String.format(Locale.getDefault(), "Length: %.02fm", hike.getLength())); - timeView.setText(String.format(Locale.getDefault(), "Time: %ds", hike.getTime())); - difficultyView.setText(res.getStringArray(R.array.hike_difficulties)[hike.getDifficulty()]); - Drawable difficultyDrawable = null; - switch (hike.getDifficulty()) { - case 0: - difficultyDrawable = res.getDrawable(R.drawable.difficulty_indicator_easy, null); - break; - case 1: - difficultyDrawable = res.getDrawable(R.drawable.difficulty_indicator_medium, null); - break; - case 2: - difficultyDrawable = res.getDrawable(R.drawable.difficulty_indicator_hard, null); - break; - } - difficultyView.setCompoundDrawablesWithIntrinsicBounds(difficultyDrawable, null, null, null); - upView.setText(String.format(Locale.getDefault(), "Up: %.02fm", hike.getHeightUp())); - downView.setText(String.format(Locale.getDefault(), "Down: %.02fm", hike.getHeightDown())); - final LineSet lineSet = new LineSet(elevationDataPointsNames, elevationDataPoints); - lineSet.setDotsColor(res.getColor(R.color.white)); - lineSet.setDotsStrokeColor(res.getColor(R.color.elevation_line)); - lineSet.setColor(res.getColor(R.color.elevation_line)); - lineSet.setFill(res.getColor(R.color.elevation_bg)); - lineSet.setDotsRadius(6); - lineSet.setDotsStrokeThickness(3); - lineSet.setThickness(4); - int minDisp = (int) min; - int maxDisp = (int) max; - int padding = (maxDisp-minDisp)/10; - minDisp = Math.max(0, minDisp-padding); - maxDisp += padding; - minDisp = Math.max(0, minDisp - (minDisp % 10)); - maxDisp += (10 - (maxDisp % 10)); - int step = (maxDisp - minDisp)/2; - elevationProfileNew.setAxisBorderValues(minDisp, maxDisp, step); - elevationProfileNew.setXLabels(AxisController.LabelPosition.NONE); - elevationProfileNew.setXAxis(false); - elevationProfileNew.setYAxis(false); - elevationProfileNew.setGrid(ChartView.GridType.HORIZONTAL, new Paint()); - elevationProfileNew.addData(lineSet); - - elevationProfileNew.setOnEntryClickListener(new OnEntryClickListener() { + }); + if(hikes.size() > 0) { + showHikesDialogBuilder + .setAdapter(new HikeListAdapter(MainActivity.this, hikes, points), new DialogInterface.OnClickListener() { @Override - public void onClick(int setIndex, int entryIndex, Rect rect) { - Toaster(lineSet.getEntry(entryIndex).getValue() + "m @ " + elevationDataPointsTimes[entryIndex], true); + public void onClick(DialogInterface dialog, int which) { + Polyline hikePath = new Polyline(MainActivity.this); + Hike hike = hikes.get(which); + List dbPoints = points.get(which); + List pathPoints = new ArrayList<>(); + float[] elevationDataPoints = new float[dbPoints.size()]; + String[] elevationDataPointsNames = new String[dbPoints.size()]; + final String[] elevationDataPointsTimes = new String[dbPoints.size()]; + double min = Double.MAX_VALUE; + double max = Double.MIN_VALUE; + long startTime = dbPoints.get(0).getTime(); + for (int i = 0; i < dbPoints.size(); i++) { + DbGeoPoint dbGeoPoint = dbPoints.get(i); + elevationDataPointsTimes[i] = formatTimeIntervalToHms((int) (dbGeoPoint.getTime() - startTime)); + Log.d(TAG, "formatted: " + elevationDataPointsTimes[i]); + double alt = dbGeoPoint.getAlt(); + pathPoints.add(new GeoPoint(dbGeoPoint.getLat(), dbGeoPoint.getLon(), alt)); + //TODO REMOVE + alt = (int) (Math.random() * 100) + 40; + elevationDataPoints[i] = (int) alt; + elevationDataPointsNames[i] = (dbGeoPoint.getTime() - startTime) + "s"; + if (alt < min) min = alt; + if (alt > max) max = alt; + } + LayoutInflater inflater = getLayoutInflater(); + View rowView = inflater.inflate(R.layout.hike_list_detail, null, true); + TextView titleView = (TextView) rowView.findViewById(R.id.hike_list_detail_title); + TextView authorView = (TextView) rowView.findViewById(R.id.hike_list_detail_author); + TextView lengthView = (TextView) rowView.findViewById(R.id.hike_list_detail_length); + TextView timeView = (TextView) rowView.findViewById(R.id.hike_list_detail_time); + TextView difficultyView = (TextView) rowView.findViewById(R.id.hike_list_detail_difficulty); + TextView upView = (TextView) rowView.findViewById(R.id.hike_list_detail_up); + TextView downView = (TextView) rowView.findViewById(R.id.hike_list_detail_down); + LineChartView elevationProfileNew = (LineChartView) rowView.findViewById(R.id.hike_list_detail_elevation_profile); + MapView hikeMap = (MapView) rowView.findViewById(R.id.hike_list_detail_map); + titleView.setText(hike.getName()); + authorView.setText(String.format(Locale.getDefault(), res.getString(R.string.hike_list_author), hike.getAuthor())); + lengthView.setText(String.format(Locale.getDefault(), "Length: %.02fm", hike.getLength())); + timeView.setText(String.format(Locale.getDefault(), "Time: %ds", hike.getTime())); + difficultyView.setText(res.getStringArray(R.array.hike_difficulties)[hike.getDifficulty()]); + Drawable difficultyDrawable = null; + switch (hike.getDifficulty()) { + case 0: + difficultyDrawable = res.getDrawable(R.drawable.difficulty_indicator_easy, null); + break; + case 1: + difficultyDrawable = res.getDrawable(R.drawable.difficulty_indicator_medium, null); + break; + case 2: + difficultyDrawable = res.getDrawable(R.drawable.difficulty_indicator_hard, null); + break; + } + difficultyView.setCompoundDrawablesWithIntrinsicBounds(difficultyDrawable, null, null, null); + upView.setText(String.format(Locale.getDefault(), "Up: %.02fm", hike.getHeightUp())); + downView.setText(String.format(Locale.getDefault(), "Down: %.02fm", hike.getHeightDown())); + final LineSet lineSet = new LineSet(elevationDataPointsNames, elevationDataPoints); + lineSet.setDotsColor(res.getColor(R.color.white)); + lineSet.setDotsStrokeColor(res.getColor(R.color.elevation_line)); + lineSet.setColor(res.getColor(R.color.elevation_line)); + lineSet.setFill(res.getColor(R.color.elevation_bg)); + lineSet.setDotsRadius(6); + lineSet.setDotsStrokeThickness(3); + lineSet.setThickness(4); + int minDisp = (int) min; + int maxDisp = (int) max; + int padding = (maxDisp - minDisp) / 10; + minDisp = Math.max(0, minDisp - padding); + maxDisp += padding; + minDisp = Math.max(0, minDisp - (minDisp % 10)); + maxDisp += (10 - (maxDisp % 10)); + int step = (maxDisp - minDisp) / 2; + elevationProfileNew.setAxisBorderValues(minDisp, maxDisp, step); + elevationProfileNew.setXLabels(AxisController.LabelPosition.NONE); + elevationProfileNew.setXAxis(false); + elevationProfileNew.setYAxis(false); + elevationProfileNew.setGrid(ChartView.GridType.HORIZONTAL, new Paint()); + elevationProfileNew.addData(lineSet); + + elevationProfileNew.setOnEntryClickListener(new OnEntryClickListener() { + @Override + public void onClick(int setIndex, int entryIndex, Rect rect) { + Toaster(lineSet.getEntry(entryIndex).getValue() + "m @ " + elevationDataPointsTimes[entryIndex], true); + } + }); + + elevationProfileNew.show(new Animation(1000)); + hikeMap.setTileSource(TileSourceFactory.MAPNIK); + hikeMap.setBuiltInZoomControls(true); + hikeMap.setMultiTouchControls(true); + IMapController hikeMapController = hikeMap.getController(); + hikeMapController.setZoom(ZOOM_LEVEL); + hikeMapController.setCenter(pathPoints.get(pathPoints.size() / 2)); + + AlertDialog detailHikeDialog = new AlertDialog.Builder(MainActivity.this) + .setTitle(String.format(Locale.getDefault(), res.getString(R.string.hike_detail_title), hike.getName())) + .setView(rowView) + .setPositiveButton(R.string.hike_start_navigation, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + } + }) + .setNegativeButton(R.string.cancel_button, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + } + }) + .create(); + detailHikeDialog.show(); + hikePath.setPoints(pathPoints); + hikePath.setColor(res.getColor(R.color.polyline_color)); + hikeMap.getOverlays().add(hikePath); + hikeMap.invalidate(); } }); - - elevationProfileNew.show(new Animation(1000)); - hikeMap.setTileSource(TileSourceFactory.MAPNIK); - hikeMap.setBuiltInZoomControls(true); - hikeMap.setMultiTouchControls(true); - IMapController hikeMapController = hikeMap.getController(); - hikeMapController.setZoom(ZOOM_LEVEL); - hikeMapController.setCenter(pathPoints.get(pathPoints.size()/2)); - - AlertDialog detailHikeDialog = new AlertDialog.Builder(MainActivity.this) - .setTitle(String.format(Locale.getDefault(), res.getString(R.string.hike_detail_title), hike.getName())) - .setView(rowView) - .setPositiveButton(R.string.hike_start_navigation, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - } - }) - .setNegativeButton(R.string.cancel_button, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - } - }) - .create(); - detailHikeDialog.show(); - hikePath.setPoints(pathPoints); - hikePath.setColor(res.getColor(R.color.polyline_color)); - hikeMap.getOverlays().add(hikePath); - hikeMap.invalidate(); - } - }) - .setTitle(R.string.hike_list_title) - .create(); - showHikesDialog.show(); + } else { + showHikesDialogBuilder.setMessage(R.string.hike_list_no_hikes); + } + AlertDialog showHikesDialog = showHikesDialogBuilder.create(); + showHikesDialog.show(); + } } } }); diff --git a/app/src/main/java/de/apps4ics/mountainnavigation/handlers/DatabaseHandler.java b/app/src/main/java/de/apps4ics/mountainnavigation/handlers/DatabaseHandler.java index f7a2ae3..1b90094 100644 --- a/app/src/main/java/de/apps4ics/mountainnavigation/handlers/DatabaseHandler.java +++ b/app/src/main/java/de/apps4ics/mountainnavigation/handlers/DatabaseHandler.java @@ -48,8 +48,9 @@ import de.apps4ics.mountainnavigation.pois.Wifi; public class DatabaseHandler extends SQLiteOpenHelper { - private static final int DATABASE_VERSION = 6; + private static final int DATABASE_VERSION = 7; private static final float MAX_POI_DISTANCE = 12.5f; + private static final float MAX_POI_DISTANCE_COS = (float) Math.cos(MAX_POI_DISTANCE / 6371); private static final String DATABASE_NAME = "mountainNavigation.db"; private static final String TABLE_GP = "geopoint"; @@ -74,7 +75,10 @@ private static final String KEY_POI_TYPE = "_type"; private static final String KEY_POI_TIME = "_time"; - private static final String KEY_GP_ID = "_gp_id"; + private static final String KEY_POI_LAT_SIN = "_lat_sin"; + private static final String KEY_POI_LAT_COS = "_lat_cos"; + private static final String KEY_POI_LON_SIN = "_lon_sin"; + private static final String KEY_POI_LON_COS = "_lon_cos"; private static final String KEY_POI_EXT_ID = "_poi_id"; private static final String KEY_FOUNTAIN_SIZE = "_size"; private static final String KEY_PATH_LENGTH = "_length"; @@ -121,6 +125,10 @@ + KEY_POI_LAT + " REAL," + KEY_POI_LON + " REAL," + KEY_POI_ALT + " REAL," + + KEY_POI_LAT_SIN + " REAL," + + KEY_POI_LAT_COS + " REAL," + + KEY_POI_LON_SIN + " REAL," + + KEY_POI_LON_COS + " REAL," + KEY_POI_EXT_ID + " INTEGER," + KEY_POI_TYPE + " INTEGER," + KEY_POI_TIME + " INTEGER" + ")"; @@ -285,6 +293,10 @@ values.put(KEY_POI_LAT, dbgp.getLat()); values.put(KEY_POI_LON, dbgp.getLon()); values.put(KEY_POI_ALT, dbgp.getAlt()); + values.put(KEY_POI_LAT_SIN, dbgp.getLatSin()); + values.put(KEY_POI_LAT_COS, dbgp.getLatCos()); + values.put(KEY_POI_LON_SIN, dbgp.getLonSin()); + values.put(KEY_POI_LON_COS, dbgp.getLonCos()); values.put(KEY_POI_TIME, dbgp.getTime()); values.put(KEY_POI_EXT_ID, dbgp.get_poiId()); values.put(KEY_POI_TYPE, dbgp.get_poiType()); @@ -345,16 +357,16 @@ } private Hike getHikeFromCursor(Cursor cursor) { - long id = cursor.getLong(0); - float length = cursor.getFloat(1); - long time = cursor.getLong(2); - int difficulty = cursor.getInt(3); - String name = cursor.getString(4); - String author = cursor.getString(5); - long timestamp = cursor.getLong(6); - float heightUp = cursor.getFloat(7); - float heightDown = cursor.getFloat(8); - int flooring = cursor.getInt(9); + long id = cursor.getLong(cursor.getColumnIndex(KEY_POI_ID)); + float length = cursor.getFloat(cursor.getColumnIndex(KEY_HIKE_LENGTH)); + long time = cursor.getLong(cursor.getColumnIndex(KEY_HIKE_TIME)); + int difficulty = cursor.getInt(cursor.getColumnIndex(KEY_HIKE_DIFFICULTY)); + String name = cursor.getString(cursor.getColumnIndex(KEY_HIKE_NAME)); + String author = cursor.getString(cursor.getColumnIndex(KEY_HIKE_AUTHOR)); + long timestamp = cursor.getLong(cursor.getColumnIndex(KEY_HIKE_TIMESTAMP)); + float heightUp = cursor.getFloat(cursor.getColumnIndex(KEY_HIKE_HEIGHT_UP)); + float heightDown = cursor.getFloat(cursor.getColumnIndex(KEY_HIKE_HEIGHT_DOWN)); + int flooring = cursor.getInt(cursor.getColumnIndex(KEY_HIKE_FLOORING)); return new Hike(id, length, time, difficulty, name, author, timestamp, heightUp, heightDown, flooring); } @@ -374,16 +386,20 @@ double lat = cursor.getDouble(cursor.getColumnIndex(KEY_POI_LAT)); double lon = cursor.getDouble(cursor.getColumnIndex(KEY_POI_LON)); double alt = cursor.getDouble(cursor.getColumnIndex(KEY_POI_ALT)); + double lat_sin = cursor.getDouble(cursor.getColumnIndex(KEY_POI_LAT_SIN)); + double lat_cos = cursor.getDouble(cursor.getColumnIndex(KEY_POI_LAT_COS)); + double lon_sin = cursor.getDouble(cursor.getColumnIndex(KEY_POI_LON_SIN)); + double lon_cos = cursor.getDouble(cursor.getColumnIndex(KEY_POI_LON_COS)); long time = cursor.getLong(cursor.getColumnIndex(KEY_POI_TIME)); long poiId = cursor.getLong(cursor.getColumnIndex(KEY_POI_EXT_ID)); int poiType = cursor.getInt(cursor.getColumnIndex(KEY_POI_TYPE)); - return new DbGeoPoint(gp_id, lat, lon, alt, time, poiId, poiType); + return new DbGeoPoint(gp_id, lat, lon, alt, lat_sin, lat_cos, lon_sin, lon_cos, time, poiId, poiType); } private DbGeoPoint getGeoPoint(long gp_id){ SQLiteDatabase db = getReadableDatabase(); Cursor gpCursor = db.query(TABLE_GP, - new String[]{KEY_POI_ID, KEY_POI_LAT, KEY_POI_LON, KEY_POI_ALT, KEY_POI_TIME, KEY_POI_EXT_ID, KEY_POI_TYPE}, + new String[]{KEY_POI_ID, KEY_POI_LAT, KEY_POI_LON, KEY_POI_ALT, KEY_POI_LAT_SIN, KEY_POI_LAT_COS, KEY_POI_LON_SIN, KEY_POI_LON_COS, KEY_POI_TIME, KEY_POI_EXT_ID, KEY_POI_TYPE}, KEY_POI_ID + "=?", new String[]{String.valueOf(gp_id)}, null, null, null, null); if(gpCursor != null){ @@ -691,61 +707,58 @@ cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM your_table_name HAVING distance < 25 ORDER BY distance LIMIT 0 , 20; + + WHERE CUR_sin_lat * sin_lat + CUR_cos_lat * cos_lat * (cos_lng* CUR_cos_lng + sin_lng * CUR_sin_lng) > cos_allowed_distance; * */ - public List getPoisAround(double lat, double lon, int limit) { + public List getPoisAround(double lat, double lon, int limit, int type) { List poisAround = new ArrayList<>(); SQLiteDatabase db = this.getReadableDatabase(); - for(String table : TABLES){ - Cursor cursor = db.query(table, - new String[]{"*", "(6371 * acos(cos(radians(" + lat + ")) * cos(radians(" + KEY_POI_LAT + ")) * cos(radians(" + KEY_POI_LON + ") - radians(" + lon + ")) + sin(radians(" + lat + ")) * sin(radians(" + KEY_POI_LAT + ")))) AS distance"}, - null, - null, - null, - "distance < " + MAX_POI_DISTANCE, //having - "distance", //orderby - limit == -1 ? null : String.valueOf(limit)); //limit - if(cursor != null) { - cursor.moveToFirst(); - do { - Poi poi = null; - switch (table) { - case TABLE_FOUNTAIN: - poi = getFountainFromCursor(cursor); - break; - case TABLE_PATH: - poi = getPathFromCursor(cursor); - break; - case TABLE_BREAK_POINT: - poi = getBreakpointFromCursor(cursor); - break; - case TABLE_TRASH_BIN: - poi = getTrashbinFromCursor(cursor); - break; - case TABLE_CELL_RECEPTION: - poi = getCellReceptionFromCursor(cursor); - break; - case TABLE_WIFI: - poi = getWifiFromCursor(cursor); - break; - case TABLE_LIFT: - poi = getLiftFromCursor(cursor); - break; - case TABLE_PEAK: - poi = getPeakFromCursor(cursor); - break; - case TABLE_HUT: - poi = getHutFromCursor(cursor); - break; - } - poisAround.add(poi); - } while(cursor.moveToNext()); - cursor.close(); - } + double lat_sin = Math.sin(lat * Math.PI / 180); + double lat_cos = Math.cos(lat * Math.PI / 180); + double lon_sin = Math.sin(lon * Math.PI / 180); + double lon_cos = Math.cos(lon * Math.PI / 180); + String whereClause = null; + String[] whereArgs = null; + if(type >= 0) { + whereClause = KEY_POI_TYPE + "=?"; + whereArgs = new String[]{ String.valueOf(type) }; + } + Cursor cursor = db.query(TABLE_GP, + null, + whereClause, + whereArgs, + KEY_POI_EXT_ID + "," + KEY_POI_TYPE, + null, + null, + limit == -1 ? null : String.valueOf(limit)); + if(cursor != null && cursor.getCount() > 0) { + cursor.moveToFirst(); + do { + DbGeoPoint dbgp = getGeoPointFromCursor(cursor); + double dist = lat_sin * dbgp.getLatSin() + lat_cos * dbgp.getLatCos() * (dbgp.getLonCos() * lon_cos + dbgp.getLonSin() * lon_sin); + if(dist < MAX_POI_DISTANCE_COS) continue; + Cursor poiCursor = db.query(getTableName(dbgp.get_poiType()), + null, + KEY_POI_ID + "=?", + new String[]{ String.valueOf(dbgp.get_poiId()) }, + null, + null, + null); + if(poiCursor != null && poiCursor.getCount() > 0 && poiCursor.moveToFirst()) { + poisAround.add(getPoiFromCursor(poiCursor, dbgp.get_poiType())); + } + poiCursor.close(); + } while(cursor.moveToNext()); + cursor.close(); } db.close(); return poisAround; } + public List getPoisAround(double lat, double lon, int limit) { + return getPoisAround(lat, lon, limit, -1); + } + public List getPoiByType(int _type){ List poiList = new ArrayList<>(); SQLiteDatabase db = this.getReadableDatabase(); diff --git a/app/src/main/java/de/apps4ics/mountainnavigation/handlers/PoiHandler.java b/app/src/main/java/de/apps4ics/mountainnavigation/handlers/PoiHandler.java index 487d77b..ff131df 100644 --- a/app/src/main/java/de/apps4ics/mountainnavigation/handlers/PoiHandler.java +++ b/app/src/main/java/de/apps4ics/mountainnavigation/handlers/PoiHandler.java @@ -88,6 +88,7 @@ import de.apps4ics.mountainnavigation.pois.CellReception; import de.apps4ics.mountainnavigation.pois.DbGeoPoint; import de.apps4ics.mountainnavigation.pois.Fountain; +import de.apps4ics.mountainnavigation.pois.Hike; import de.apps4ics.mountainnavigation.pois.Image; import de.apps4ics.mountainnavigation.pois.Lift; import de.apps4ics.mountainnavigation.pois.Options; @@ -235,6 +236,14 @@ poiMarkers[type].add(marker); } + public List getPoisAround(Location l) { + return getPoisAround(l.getLatitude(), l.getLongitude()); + } + + public List getPoisAround(Location l, int limit) { + return getPoisAround(l.getLatitude(), l.getLongitude(), limit); + } + public List getPoisAround(double lat, double lon) { return getPoisAround(lat, lon, -1); } @@ -277,6 +286,19 @@ } + public List getHikesAround(Location l) { + return getHikesAround(l.getLatitude(), l.getLongitude()); + } + + public List getHikesAround(double lat, double lon) { + List hikes = new ArrayList<>(); + List entries = dbHandler.getPoisAround(lat, lon, -1, Types.HIKE); + for(Poi p : entries) { + hikes.add((Hike) p); + } + return hikes; + } + public void updatePoi(Poi poi) { updatePoiInCache(poi); if(MainActivity.hasInternet()) updatePoiInOsm(poi); diff --git a/app/src/main/java/de/apps4ics/mountainnavigation/pois/DbGeoPoint.java b/app/src/main/java/de/apps4ics/mountainnavigation/pois/DbGeoPoint.java index 6330b2c..ccee528 100644 --- a/app/src/main/java/de/apps4ics/mountainnavigation/pois/DbGeoPoint.java +++ b/app/src/main/java/de/apps4ics/mountainnavigation/pois/DbGeoPoint.java @@ -27,6 +27,10 @@ private double lat; private double lon; private double alt; + private double lat_sin; + private double lat_cos; + private double lon_sin; + private double lon_cos; private long _time; private long _poiId; private int _poiType; @@ -59,11 +63,23 @@ this(id, gp.getLatitude(), gp.getLongitude(), gp.getAltitude(), time, _poiId, _poiType); } + public DbGeoPoint(long id, double lat, double lon, double alt, double lat_sin, double lat_cos, double lon_sin, double lon_cos, long time, long _poiId, int _poiType) { + this(id, lat, lon, alt, time, _poiId, _poiType); + this.lat_sin = lat_sin; + this.lat_cos = lat_cos; + this.lon_sin = lon_sin; + this.lon_cos = lon_cos; + } + public DbGeoPoint(long id, double lat, double lon, double alt, long time, long _poiId, int _poiType){ super(id, Types.GP); this.lat = lat; this.lon = lon; this.alt = alt; + this.lat_sin = Math.sin(lat * Math.PI / 180); + this.lat_cos = Math.cos(lat * Math.PI / 180); + this.lon_sin = Math.sin(lon * Math.PI / 180); + this.lon_cos = Math.cos(lon * Math.PI / 180); this._time = time; this._poiId = _poiId; this._poiType = _poiType; @@ -104,4 +120,36 @@ public void set_poiId(long _poiId) { this._poiId = _poiId; } + + public double getLonCos() { + return lon_cos; + } + + public void setLonCos(double lon_cos) { + this.lon_cos = lon_cos; + } + + public double getLonSin() { + return lon_sin; + } + + public void setLonSin(double lon_sin) { + this.lon_sin = lon_sin; + } + + public double getLatCos() { + return lat_cos; + } + + public void setLatCos(double lat_cos) { + this.lat_cos = lat_cos; + } + + public double getLatSin() { + return lat_sin; + } + + public void setLatSin(double lat_sin) { + this.lat_sin = lat_sin; + } } diff --git a/app/src/main/res/layout/overlay_custom_bubble.xml b/app/src/main/res/layout/overlay_custom_bubble.xml index 69c7040..94fbc3d 100644 --- a/app/src/main/res/layout/overlay_custom_bubble.xml +++ b/app/src/main/res/layout/overlay_custom_bubble.xml @@ -30,6 +30,7 @@ android:layout_height="50dp" android:paddingRight="6dp" android:paddingEnd="6dp" + android:layout_gravity="center_vertical" android:id="@id/bubble_image"/> OK Weiter Abbrechen + Schließen WLAN aktivieren Deaktiviert lassen Welche Art von POI möchtest du hinzufügen? @@ -87,6 +88,7 @@ Wanderung starten Eigenschaften von %1$s Wanderungen in deiner Nähe + Keine Wanderungen in deiner Nähe von %1$s Starte Navigation diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f1383ce..4bf0fc3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -24,6 +24,7 @@ Okay Continue Cancel + Close Enable WIFI Stay disabled What kind of POI do you want to add? @@ -89,6 +90,7 @@ Start hike Details of %1$s Hikes around you + No hikes around you from %1$s Start navigation