-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMapStateListener.java
More file actions
125 lines (107 loc) · 3.29 KB
/
Copy pathMapStateListener.java
File metadata and controls
125 lines (107 loc) · 3.29 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
116
117
118
119
120
121
122
123
124
125
package dk.composed.mapstate;
import android.app.Activity;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.CameraPosition;
import java.util.Timer;
import java.util.TimerTask;
public abstract class MapStateListener {
private boolean mMapTouched = false;
private boolean mMapSettled = false;
private Timer mTimer;
private static final int SETTLE_TIME = 500;
private GoogleMap mMap;
private CameraPosition mLastPosition;
private Activity mActivity;
public MapStateListener(GoogleMap map, TouchableMapFragment touchableMapFragment, Activity activity) {
this.mMap = map;
this.mActivity = activity;
map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
unsettleMap();
if(!mMapTouched) {
runSettleTimer();
}
}
});
touchableMapFragment.setTouchListener(new TouchableWrapper.OnTouchListener() {
@Override
public void onTouch() {
touchMap();
unsettleMap();
}
@Override
public void onRelease() {
releaseMap();
runSettleTimer();
}
});
}
private void updateLastPosition() {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
mLastPosition = MapStateListener.this.mMap.getCameraPosition();
}
});
}
private void runSettleTimer() {
updateLastPosition();
if(mTimer != null) {
mTimer.cancel();
mTimer.purge();
}
mTimer = new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
CameraPosition currentPosition = MapStateListener.this.mMap.getCameraPosition();
if (currentPosition.equals(mLastPosition)) {
settleMap();
}
}
});
}
}, SETTLE_TIME);
}
private synchronized void releaseMap() {
if(mMapTouched) {
mMapTouched = false;
onMapReleased();
}
}
private void touchMap() {
if(!mMapTouched) {
if(mTimer != null) {
mTimer.cancel();
mTimer.purge();
}
mMapTouched = true;
onMapTouched();
}
}
public void unsettleMap() {
if(mMapSettled) {
if(mTimer != null) {
mTimer.cancel();
mTimer.purge();
}
mMapSettled = false;
mLastPosition = null;
onMapUnsettled();
}
}
public void settleMap() {
if(!mMapSettled) {
mMapSettled = true;
onMapSettled();
}
}
public abstract void onMapTouched();
public abstract void onMapReleased();
public abstract void onMapUnsettled();
public abstract void onMapSettled();
}