-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCustomCoordinator.java
More file actions
115 lines (81 loc) · 3.57 KB
/
CustomCoordinator.java
File metadata and controls
115 lines (81 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package io.maincraft.mapaeffects;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.Projection;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import java.util.LinkedHashMap;
public class CustomCoordinator extends FrameLayout {
public static GoogleMap mMap;
public static LinkedHashMap<Marker, Boolean> map = new LinkedHashMap<Marker, Boolean>();
public CustomCoordinator(Context context) {
super(context);
}
public CustomCoordinator(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomCoordinator(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomCoordinator(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
Projection projection = mMap.getProjection();
for(LinkedHashMap.Entry<Marker, Boolean> entry : map.entrySet()) {
Marker marker = entry.getKey();
LatLng markerLocation = marker.getPosition();
Point screenPosition = projection.toScreenLocation(markerLocation);
int cursorX = Math.round(event.getX());
int cursorY = Math.round(event.getY());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
cursorX = Math.round(event.getX());
cursorY = Math.round(event.getY());
int MarkerX = screenPosition.x;
int MarkerY = screenPosition.y;
if(Math.abs(MarkerX-cursorX) <= 150 && Math.abs(MarkerY-cursorY) <=150 ) {
entry.setValue(true);
mMap.getUiSettings().setScrollGesturesEnabled(false);
}
break;
case MotionEvent.ACTION_UP:
entry.setValue(false);
mMap.getUiSettings().setScrollGesturesEnabled(true);
break;
case MotionEvent.ACTION_MOVE:
cursorX = Math.round(event.getX());
cursorY = Math.round(event.getY());
Point touchPoint = new Point();
touchPoint.set(cursorX, cursorY);
LatLng latLng = projection.fromScreenLocation(touchPoint);
if(entry.getValue()) {
entry.getKey().setPosition(latLng);
redrawLines();
}
break;
}
}
return super.dispatchTouchEvent(event);
}
public static Polyline line;
public static void redrawLines() {
if(line != null) {
line.remove();
}
PolylineOptions lineOptions = new PolylineOptions();
for(LinkedHashMap.Entry<Marker, Boolean> entry : map.entrySet()) {
lineOptions.add(entry.getKey().getPosition()).width(15).color(Color.GREEN);
}
lineOptions.add(map.entrySet().iterator().next().getKey().getPosition()).width(15).color(Color.GREEN);
line =mMap.addPolyline(lineOptions);
}
}