Reputation: 57
I'm trying to drag ImageView
to google map and drop it as a marker.
Map is in FrameLayout
:
<FrameLayout
android:id="@+id/map_frg"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2">
I've set to this FrameLayout
OnDragListener
:
private class PinDragListener implements View.OnDragListener {
private GoogleMap map;
public PinDragListener(GoogleMap map) {
this.map = map;
}
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DROP:
MarkerOptions markerOptions = new MarkerOptions()
.draggable(true)
.anchor(0.0f, 1.0f);
int[] coords = new int[2];
v.getLocationOnScreen(coords);
Projection projection = map.getProjection();
LatLng latLng = projection.fromScreenLocation(new Point(coords[0], coords[1]));
markerOptions.position(latLng);
markerOptions.visible(true);
map.addMarker(markerOptions);
break;
default:
break;
}
return true;
}
}
But when image was dropped somewhere marker placed in the left corner again and again. What can be wrong?
UPDATE:
I've passed dragged view via listener
private class PinTouchListener implements OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(data, shadowBuilder, v, 0);
dragPinListener.onViewTouched(v);
return true;
} else {
return false;
}
}
}
and I've got such coordinates every time I dropped marker
x = 0 | y = 1234
Lat = 65.96291676309829 | Lng = -18.548969998955727
Upvotes: 0
Views: 638
Reputation: 2998
The OnDragListener.onDrag() method will get called for each specific event that occurs during the drag-and-drag process, passing in a DragEvent to describe the specifics of each event. Each DragEvent will have one of the following actions:
ACTION_DRAG_LOCATION : Sent to a view between ACTION_DRAG_ENTERED and ACTION_DRAG_EXITED with the current location of the drag inside that view. ・ The location can be obtained with getX() and getY().
How It Works Here are example of the drag-and-drop functionality, We have created a custom ImageView that implements the OnDragListener interface.
Please refer to the following this link: http://www.tutorialspoint.com/android/android_drag_and_drop.htm https://github.com/wada811/Android-View-Drag-And-Drop-Sample/blob/master/app/src/main/java/com/wada811/android_view_drag_and_drop_sample/DragAndDropFrameLayout.java
Hope it helps.
Upvotes: 1
Reputation: 57
I need to use event.getX()
and event.getY()
in onDrag
method in OnDragListener
to retrieve current view screen location.
Upvotes: 0