-
Notifications
You must be signed in to change notification settings - Fork 982
/
SampleAnimateTo.java
150 lines (130 loc) · 4.8 KB
/
SampleAnimateTo.java
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package org.osmdroid.samplefragments.events;
import android.util.DisplayMetrics;
import android.widget.Toast;
import org.osmdroid.R;
import org.osmdroid.data.DataRegion;
import org.osmdroid.data.DataRegionLoader;
import org.osmdroid.util.BoundingBox;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.overlay.ScaleBarOverlay;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
/**
* https://github.com/osmdroid/osmdroid/issues/264
* extends gridlines to provide visual confirmation
* Created by alex on 2/22/16.
*/
public class SampleAnimateTo extends SampleMapEventListener {
private int mIndex;
private ScaleBarOverlay mScaleBarOverlay;
private Timer t = new Timer();
private boolean alive = true;
private final List<DataRegion> mList = new ArrayList<>();
@Override
public String getSampleTitle() {
return "Animate To";
}
@Override
public void addOverlays() {
super.addOverlays();
final DisplayMetrics dm = getActivity().getResources().getDisplayMetrics();
mScaleBarOverlay = new ScaleBarOverlay(mMapView);
mScaleBarOverlay.setCentred(true);
mScaleBarOverlay.setScaleBarOffset(dm.widthPixels / 2, 10);
mMapView.getOverlays().add(mScaleBarOverlay);
// according to https://www.flickr.com/places/info/12589342
final GeoPoint manhattanCenter = new GeoPoint(40.7909, -73.9664);
final BoundingBox manhattanBoundingBox = new BoundingBox(40.8820, -73.9067, 40.6829, -74.0479);
// testing a "single point bounding box" (actually using zoom fallback in animateTo)
mList.add(new DataRegion("dummy1", "Manhattan - single point",
new BoundingBox(manhattanCenter.getLatitude(), manhattanCenter.getLongitude(), manhattanCenter.getLatitude(), manhattanCenter.getLongitude())));
// testing a "single latitude bounding box"
mList.add(new DataRegion("dummy2", "Manhattan - single latitude",
new BoundingBox(manhattanCenter.getLatitude(), manhattanBoundingBox.getLonEast(), manhattanCenter.getLatitude(), manhattanBoundingBox.getLonWest())));
// testing a "single longitude bounding box"
mList.add(new DataRegion("dummy3", "Manhattan - single longitude",
new BoundingBox(manhattanBoundingBox.getLatNorth(), manhattanCenter.getLongitude(), manhattanBoundingBox.getLatSouth(), manhattanCenter.getLongitude())));
// testing a "single longitude bounding box"
mList.add(new DataRegion("dummy4", "Manhattan - box", manhattanBoundingBox));
try {
mList.addAll(new DataRegionLoader(getActivity(), R.raw.data_region_usstates).getList().values());
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
mMapView.post(new Runnable() {
@Override
public void run() {
show();
}
});
}
@Override
public void onResume() {
super.onResume();
alive = true;
//some explanation here.
//we using a timer task with a delayed start up to move the map around. during CI tests
//this fragment can crash the app if you navigate away from the fragment before the initial fire
final TimerTask task = new TimerTask() {
@Override
public void run() {
runTask();
}
};
t = new Timer();
t.schedule(task, 4000, 4000);
}
@Override
public void onPause() {
super.onPause();
alive = false;
if (t != null)
t.cancel();
t = null;
}
private void runTask() {
if (!alive)
return;
if (getActivity() == null) {
return;
}
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (mMapView == null || getActivity() == null) {
return;
}
show();
}
});
}
/**
* @since 6.0.2
*/
@Override
public void onDestroyView() {
super.onDestroyView();
mScaleBarOverlay = null;
}
/**
* @since 6.0.2
*/
private void show() {
show(mIndex++);
}
/**
* @since 6.0.2
*/
private void show(final int pIndex) {
final int borderSizeInPixels = 20;
final double zoomFallback = 12;
final long animationSpeed = 2000;
final boolean animated = true;
final DataRegion state = mList.get(pIndex % mList.size());
final BoundingBox box = state.getBox();
mMapView.zoomToBoundingBox(box, animated, borderSizeInPixels, zoomFallback, animationSpeed);
Toast.makeText(getActivity(), state.getName(), Toast.LENGTH_SHORT).show();
}
}