Skip to content

Commit 43d0bb8

Browse files
committed
Merge branch 'dev'
2 parents 93f5c83 + e02f8da commit 43d0bb8

File tree

8 files changed

+132
-114
lines changed

8 files changed

+132
-114
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ android {
1010
applicationId "de.hauke_stieler.geonotes"
1111
minSdkVersion 16
1212
targetSdkVersion 30
13-
versionCode 1004000
14-
versionName "1.4.0"
13+
versionCode 1004001
14+
versionName "1.4.1"
1515

1616
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1717
}

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
android:roundIcon="@mipmap/ic_launcher"
2121
android:supportsRtl="true"
2222
android:theme="@style/Theme.GeoNotes">
23-
<activity android:name=".note_list.NoteListActivity"></activity>
23+
<activity android:name=".note_list.NoteListActivity" />
2424
<activity
2525
android:name=".settings.SettingsActivity"
2626
android:label="@string/title_activity_settings" />

app/src/main/java/de/hauke_stieler/geonotes/map/Map.java

Lines changed: 87 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import android.graphics.drawable.Drawable;
88
import android.os.PowerManager;
99
import android.util.DisplayMetrics;
10+
import android.view.DragEvent;
1011
import android.view.MotionEvent;
12+
import android.view.View;
1113

1214
import androidx.core.content.res.ResourcesCompat;
1315

@@ -30,9 +32,7 @@
3032
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay;
3133

3234
import java.io.File;
33-
import java.util.ArrayList;
3435
import java.util.List;
35-
import java.util.stream.Collectors;
3636

3737
import de.hauke_stieler.geonotes.database.Database;
3838
import de.hauke_stieler.geonotes.R;
@@ -50,7 +50,6 @@ public class Map {
5050

5151
private MarkerWindow markerInfoWindow;
5252
private Marker.OnMarkerClickListener markerClickListener;
53-
private Marker markerToMove;
5453

5554
private final Drawable normalIcon;
5655
private final Drawable normalWithPhotoIcon;
@@ -59,6 +58,10 @@ public class Map {
5958

6059
private boolean snapNoteToGps;
6160

61+
// Variables used during moving a marker. Do not use when no marker is currently in move mode (aka when markerToMove==null)
62+
private Marker markerToMove;
63+
private Point dragStartMarkerPosition;
64+
6265
public Map(Context context,
6366
MapView map,
6467
Database database) {
@@ -121,43 +124,23 @@ private void createOverlays(Context context, MapView map, BitmapDrawable locatio
121124

122125
// Add marker click listener. Will be called when the user clicks/taps on a marker.
123126
markerClickListener = (marker, mapView) -> {
124-
// When we are in the state of moving an existing marker, we do not want to interact with other markers -> simply return
125-
if (markerToMove != null) {
126-
return true;
127-
}
128-
129127
selectMarker(marker);
130-
131128
return true;
132129
};
133130

134131
// React to touches on the map
135132
MapEventsReceiver mapEventsReceiver = new MapEventsReceiver() {
136133
@Override
137134
public boolean singleTapConfirmedHelper(GeoPoint p) {
138-
// When we have a marker to move, set its new position, store that and disable move-state
139-
if (markerToMove != null) {
140-
markerToMove.setPosition(p);
141-
selectMarker(markerToMove);
142-
143-
// If the ID is set, the marker exists in the DB, therefore we store that new location
144-
String id = markerToMove.getId();
145-
if (id != null) {
146-
database.updateLocation(Long.parseLong(id), p);
147-
}
148-
149-
markerToMove = null;
135+
// No marker to move here -> deselect or create marker
136+
// (selecting marker on the map is handles via the separate markerClickListener)
137+
if (markerInfoWindow.getSelectedMarker() != null) {
138+
// Deselect selected marker:
139+
setNormalIcon(markerInfoWindow.getSelectedMarker());
140+
markerInfoWindow.close();
150141
} else {
151-
// No marker to move here -> deselect or create marker
152-
// (selecting marker on the map is handles via the separate markerClickListener)
153-
if (markerInfoWindow.getSelectedMarker() != null) {
154-
// Deselect selected marker:
155-
setNormalIcon(markerInfoWindow.getSelectedMarker());
156-
markerInfoWindow.close();
157-
} else {
158-
// No marker currently selected -> create new marker at this location
159-
initAndSelectMarker(p);
160-
}
142+
// No marker currently selected -> create new marker at this location
143+
initAndSelectMarker(p);
161144
}
162145

163146
return false;
@@ -175,8 +158,35 @@ public boolean longPressHelper(GeoPoint p) {
175158
public void addMapListener(MapListener listener, TouchDownListener touchDownListener) {
176159
map.addMapListener(listener);
177160
map.setOnTouchListener((v, event) -> {
178-
if (event.getAction() == MotionEvent.ACTION_DOWN) {
179-
touchDownListener.onTouchDown();
161+
switch (event.getAction()) {
162+
case MotionEvent.ACTION_DOWN:
163+
touchDownListener.onTouchDown();
164+
165+
// Initialize movement of the marker: Store current screen-location to keep marker there
166+
if (markerToMove != null) {
167+
dragStartMarkerPosition = map.getProjection().toPixels(markerToMove.getPosition(), null);
168+
}
169+
break;
170+
case MotionEvent.ACTION_MOVE:
171+
// When in drag-mode: Keep marker at original screen location by setting its position
172+
if (markerToMove != null && dragStartMarkerPosition != null) {
173+
markerToMove.setPosition((GeoPoint) map.getProjection().fromPixels(dragStartMarkerPosition.x, dragStartMarkerPosition.y));
174+
}
175+
break;
176+
case MotionEvent.ACTION_UP:
177+
if (markerToMove != null) {
178+
selectMarker(markerToMove);
179+
180+
// If the ID is set, the marker exists in the DB, therefore we store that new location
181+
String id = markerToMove.getId();
182+
if (id != null) {
183+
database.updateLocation(Long.parseLong(id), markerToMove.getPosition());
184+
}
185+
186+
dragStartMarkerPosition = null;
187+
markerToMove = null;
188+
}
189+
break;
180190
}
181191
return false;
182192
});
@@ -203,7 +213,14 @@ public void onSave(Marker marker) {
203213
@Override
204214
public void onMove(Marker marker) {
205215
markerToMove = marker;
206-
// The new position is determined and stored in the click handler of the map
216+
// The new position is determined and stored in the onTouch-handler of the map
217+
}
218+
219+
@Override
220+
public void onTextChanged() {
221+
if (getSelectedMarker() != null) {
222+
moveMapWithMarkerWindowOnTop(getSelectedMarker());
223+
}
207224
}
208225
});
209226
}
@@ -272,7 +289,40 @@ private void selectMarker(Marker marker) {
272289

273290
addImagesToMarkerWindow();
274291

275-
zoomToLocation(marker.getPosition(), map.getZoomLevelDouble());
292+
// Fragment not yet drawn, so we have to measure the height manually
293+
markerInfoWindow.getView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
294+
moveMapWithMarkerWindowOnTop(marker);
295+
}
296+
297+
private void moveMapWithMarkerWindowOnTop(Marker marker) {
298+
int markerWindowHeight = markerInfoWindow.getView().getMeasuredHeight();
299+
300+
Point markerPositionPixel = new Point();
301+
map.getProjection().toPixels(marker.getPosition(), markerPositionPixel);
302+
303+
/*
304+
* Center of the screen in relation to the y-coordinate of the marker such that the window
305+
* is 20px below the top-edge of the map.
306+
* _________________
307+
* | ___________ |
308+
* | |___________| |
309+
* | |_| | _ --> markerPositionPixel.y
310+
* | | |
311+
* | | |- distance we have to add to markerPositionPixel.y
312+
* | | _|
313+
* | X | --> center of the screen = Y-coordinate we need to find
314+
* | |
315+
* | |
316+
* | |
317+
* | |
318+
* |_________________|
319+
*/
320+
int yCoordinate = markerPositionPixel.y + (map.getHeight() / 2 - markerWindowHeight - marker.getIcon().getIntrinsicHeight() - 20);
321+
322+
Point locationInPixels = new Point(markerPositionPixel.x, yCoordinate);
323+
IGeoPoint newPoint = map.getProjection().fromPixels(locationInPixels.x, locationInPixels.y);
324+
325+
zoomToLocation(newPoint, map.getZoomLevelDouble());
276326
}
277327

278328
private Marker getSelectedMarker() {
@@ -326,12 +376,8 @@ public void setMapScaleFactor(float factor) {
326376
map.setTilesScaleFactor(factor);
327377
}
328378

329-
private void zoomToLocation(GeoPoint p, double zoom) {
330-
Point locationInPixels = new Point();
331-
map.getProjection().toPixels(p, locationInPixels);
332-
IGeoPoint newPoint = map.getProjection().fromPixels(locationInPixels.x, locationInPixels.y);
333-
334-
mapController.setCenter(newPoint);
379+
private void zoomToLocation(IGeoPoint p, double zoom) {
380+
mapController.setCenter(new GeoPoint(p));
335381
mapController.setZoom(zoom);
336382
}
337383

0 commit comments

Comments
 (0)