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