|
@@ -0,0 +1,111 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2009-2010 Chris Smith
|
|
3
|
+ *
|
|
4
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+ * in the Software without restriction, including without limitation the rights
|
|
7
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+ * furnished to do so, subject to the following conditions:
|
|
10
|
+ *
|
|
11
|
+ * The above copyright notice and this permission notice shall be included in
|
|
12
|
+ * all copies or substantial portions of the Software.
|
|
13
|
+ *
|
|
14
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+ * SOFTWARE.
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+package uk.co.md87.android.placesdisplay;
|
|
24
|
+
|
|
25
|
+import android.content.Context;
|
|
26
|
+import android.database.Cursor;
|
|
27
|
+import android.graphics.Canvas;
|
|
28
|
+import android.graphics.Paint;
|
|
29
|
+import android.graphics.Path;
|
|
30
|
+import android.graphics.Point;
|
|
31
|
+import android.util.Log;
|
|
32
|
+import com.google.android.maps.MapView;
|
|
33
|
+import com.google.android.maps.Overlay;
|
|
34
|
+import com.google.android.maps.OverlayItem;
|
|
35
|
+import java.util.ArrayList;
|
|
36
|
+import java.util.HashMap;
|
|
37
|
+import java.util.List;
|
|
38
|
+import java.util.Map;
|
|
39
|
+import uk.co.md87.android.contextapi.ContextApi;
|
|
40
|
+import uk.co.md87.android.contextapi.ContextApi.Journeys.ColumnNames;
|
|
41
|
+
|
|
42
|
+/**
|
|
43
|
+ * An overlay for showing journeys between places.
|
|
44
|
+ *
|
|
45
|
+ * @author chris
|
|
46
|
+ */
|
|
47
|
+public class JourneysOverlay extends Overlay {
|
|
48
|
+
|
|
49
|
+ private final Map<OverlayItem, Map<OverlayItem, Integer>> journeys
|
|
50
|
+ = new HashMap<OverlayItem, Map<OverlayItem, Integer>>();
|
|
51
|
+ private int max = 0;
|
|
52
|
+
|
|
53
|
+ public JourneysOverlay(final Context context, final Map<Integer, OverlayItem> places) {
|
|
54
|
+ final Cursor cursor = context.getContentResolver().query(ContextApi.Journeys.CONTENT_URI,
|
|
55
|
+ new String[] { ColumnNames.START, ColumnNames.END, ColumnNames.NUMBER },
|
|
56
|
+ null, null, null);
|
|
57
|
+
|
|
58
|
+ if (cursor.moveToFirst()) {
|
|
59
|
+ final int startColumn = cursor.getColumnIndex(ColumnNames.START);
|
|
60
|
+ final int endColumn = cursor.getColumnIndex(ColumnNames.END);
|
|
61
|
+ final int numberColumn = cursor.getColumnIndex(ColumnNames.NUMBER);
|
|
62
|
+
|
|
63
|
+ do {
|
|
64
|
+ final int start = cursor.getInt(startColumn);
|
|
65
|
+ final int end = cursor.getInt(endColumn);
|
|
66
|
+ final OverlayItem a = places.get(Math.min(start, end)),
|
|
67
|
+ b = places.get(Math.max(start, end));
|
|
68
|
+
|
|
69
|
+ if (!journeys.containsKey(a)) {
|
|
70
|
+ journeys.put(a, new HashMap<OverlayItem, Integer>());
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+ final Map<OverlayItem, Integer> map = journeys.get(a);
|
|
74
|
+
|
|
75
|
+ if (!map.containsKey(b)) {
|
|
76
|
+ map.put(b, 0);
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ final int total = map.get(b) + cursor.getInt(numberColumn);
|
|
80
|
+ map.put(b, total);
|
|
81
|
+ max = Math.max(max, total);
|
|
82
|
+ } while (cursor.moveToNext());
|
|
83
|
+ }
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ @Override
|
|
87
|
+ public void draw(Canvas canvas, MapView map, boolean shadow) {
|
|
88
|
+ super.draw(canvas, map, shadow);
|
|
89
|
+
|
|
90
|
+ final Paint paint = new Paint();
|
|
91
|
+ paint.setARGB(255, 255, 0, 0);
|
|
92
|
+ paint.setStyle(Paint.Style.FILL_AND_STROKE);
|
|
93
|
+
|
|
94
|
+ for (Map.Entry<OverlayItem, Map<OverlayItem, Integer>> start
|
|
95
|
+ : journeys.entrySet()) {
|
|
96
|
+ final Point spoint = map.getProjection().toPixels(start.getKey().getPoint(), null);
|
|
97
|
+ for (Map.Entry<OverlayItem, Integer> entry : start.getValue().entrySet()) {
|
|
98
|
+ final Point epoint = map.getProjection().toPixels(entry.getKey().getPoint(), null);
|
|
99
|
+
|
|
100
|
+ final Paint mypaint = new Paint(paint);
|
|
101
|
+
|
|
102
|
+ mypaint.setStrokeJoin(Paint.Join.ROUND);
|
|
103
|
+ mypaint.setStrokeCap(Paint.Cap.ROUND);
|
|
104
|
+ mypaint.setStrokeWidth(10 * entry.getValue() / (float) max);
|
|
105
|
+
|
|
106
|
+ canvas.drawLine(spoint.x, spoint.y, epoint.x, epoint.y, mypaint);
|
|
107
|
+ }
|
|
108
|
+ }
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+}
|