3
3
using System . Linq ;
4
4
using System . Threading ;
5
5
using System . Threading . Tasks ;
6
-
6
+ using Android . Animation ;
7
7
using Android . Gms . Maps ;
8
8
using Android . Gms . Maps . Model ;
9
9
using Android . Graphics ;
13
13
using Android . Utilities ;
14
14
using Android . Views ;
15
15
16
- using Java . Lang ;
16
+ using Activity = Android . App . Activity ;
17
17
18
18
namespace TramUrWay . Android
19
19
{
20
- public class LineMapFragment : TabFragment , IOnMapReadyCallback
20
+ public class LineMapFragment : TabFragment , IOnMapReadyCallback , GoogleMap . IOnMapLoadedCallback
21
21
{
22
+ public class MarkerAnimator : Java . Lang . Object , ValueAnimator . IAnimatorUpdateListener
23
+ {
24
+ private Activity activity ;
25
+ private Marker marker ;
26
+ private Transport transport ;
27
+ private Action < LatLng > positionUpdater ;
28
+
29
+ public MarkerAnimator ( Activity activity , Marker marker , Transport transport , Action < LatLng > positionUpdater )
30
+ {
31
+ this . activity = activity ;
32
+ this . marker = marker ;
33
+ this . transport = transport ;
34
+ this . positionUpdater = positionUpdater ;
35
+ }
36
+
37
+ public void OnAnimationUpdate ( ValueAnimator animation )
38
+ {
39
+ float progress = transport . Progress + ( transport . NextProgress - transport . Progress ) * animation . AnimatedFraction ;
40
+ int index = transport . Step . Trajectory . TakeWhile ( s => s . Index <= progress ) . Count ( ) ;
41
+
42
+ bool last = index >= transport . Step . Trajectory . Length ;
43
+ TrajectoryStep from = transport . Step . Trajectory [ index - 1 ] ;
44
+ TrajectoryStep to = last ? transport . TimeStep . Step . Trajectory . First ( ) : transport . Step . Trajectory [ index ] ;
45
+
46
+ progress = ( progress - from . Index ) / ( ( last ? 1 : to . Index ) - from . Index ) ;
47
+ LatLng position = new LatLng ( from . Position . Latitude + ( to . Position . Latitude - from . Position . Latitude ) * progress , from . Position . Longitude + ( to . Position . Longitude - from . Position . Longitude ) * progress ) ;
48
+
49
+ positionUpdater ( position ) ;
50
+ }
51
+ }
52
+
22
53
private const int StopIconSize = 10 ;
23
54
private const int TransportIconSize = 22 ;
24
55
@@ -29,6 +60,7 @@ public class LineMapFragment : TabFragment, IOnMapReadyCallback
29
60
30
61
private SupportMapFragment mapFragment ;
31
62
private GoogleMap googleMap ;
63
+ private Dictionary < Transport , Marker > transportMarkers = new Dictionary < Transport , Marker > ( ) ;
32
64
33
65
private BitmapDescriptor stopBitmapDescriptor ;
34
66
private BitmapDescriptor transportBitmapDescriptor ;
@@ -67,56 +99,18 @@ public void OnMapReady(GoogleMap googleMap)
67
99
googleMap . UiSettings . MapToolbarEnabled = true ;
68
100
69
101
// Register events
102
+ mapFragment . View . Post ( OnMapLoaded ) ;
70
103
//googleMap.CameraChange += GoogleMap_CameraChange;
71
104
//googleMap.MarkerClick += GoogleMap_MarkerClick;
72
105
//googleMap.MapClick += GoogleMap_MapClick;
73
106
74
107
// Preload icons
75
108
Task iconLoader = Task . Run ( ( ) =>
76
109
{
77
- float density = Resources . DisplayMetrics . Density ;
78
- Paint paint = new Paint ( ) ;
79
-
80
- // Station icon
81
- int stopIconSize = ( int ) ( StopIconSize * density ) ;
82
- Bitmap stopBitmap = Bitmap . CreateBitmap ( stopIconSize , stopIconSize , Bitmap . Config . Argb8888 ) ;
83
- Canvas stopCanvas = new Canvas ( stopBitmap ) ;
84
-
85
- paint . SetARGB ( color . A , color . R , color . G , color . B ) ;
86
- stopCanvas . DrawCircle ( stopIconSize / 2 , stopIconSize / 2 , stopIconSize / 2 , paint ) ;
87
-
88
- paint . SetARGB ( 0xFF , 0xFF , 0xFF , 0xFF ) ;
89
- stopCanvas . DrawCircle ( stopIconSize / 2 , stopIconSize / 2 , stopIconSize / 2 - ( int ) ( density * 2 ) , paint ) ;
90
-
91
- stopBitmapDescriptor = BitmapDescriptorFactory . FromBitmap ( stopBitmap ) ;
92
-
93
- // Line icon
94
- int transportIconSize = ( int ) ( TransportIconSize * density ) ;
95
- Drawable transportDrawable = Resources . GetDrawable ( Resource . Drawable . train ) ;
96
- Drawable transportDrawableOutline = Resources . GetDrawable ( Resource . Drawable . train_glow ) ;
97
-
98
- Bitmap transportBitmap = Bitmap . CreateBitmap ( transportIconSize , transportIconSize , Bitmap . Config . Argb8888 ) ;
99
- Canvas transportCanvas = new Canvas ( transportBitmap ) ;
100
-
101
- transportDrawableOutline . SetBounds ( 0 , 0 , transportIconSize , transportIconSize ) ;
102
- transportDrawableOutline . Draw ( transportCanvas ) ;
103
-
104
- transportDrawable . SetColorFilter ( color , PorterDuff . Mode . SrcIn ) ;
105
- transportDrawable . SetBounds ( 0 , 0 , transportIconSize , transportIconSize ) ;
106
- transportDrawable . Draw ( transportCanvas ) ;
107
-
108
- transportBitmapDescriptor = BitmapDescriptorFactory . FromBitmap ( transportBitmap ) ;
110
+ stopBitmapDescriptor = BitmapDescriptorFactory . FromBitmap ( Utils . GetStopIconForLine ( Activity , line , StopIconSize ) ) ;
111
+ transportBitmapDescriptor = BitmapDescriptorFactory . FromBitmap ( Utils . GetTransportIconForLine ( Activity , line , TransportIconSize ) ) ;
109
112
} ) ;
110
113
111
- // Compute global line bounds to initialize camera
112
- LatLngBounds . Builder boundsBuilder = new LatLngBounds . Builder ( ) ;
113
- foreach ( Route route in line . Routes )
114
- foreach ( Step step in route . Steps )
115
- boundsBuilder . Include ( new LatLng ( step . Stop . Position . Latitude , step . Stop . Position . Longitude ) ) ;
116
-
117
- CameraUpdate cameraUpdate = CameraUpdateFactory . NewLatLngBounds ( boundsBuilder . Build ( ) , 100 ) ;
118
- googleMap . MoveCamera ( cameraUpdate ) ;
119
-
120
114
// Add a polyline between steps
121
115
foreach ( Route route in line . Routes )
122
116
{
@@ -154,12 +148,38 @@ public void OnMapReady(GoogleMap googleMap)
154
148
googleMap . AddMarker ( marker ) ;
155
149
}
156
150
}
151
+ public void OnMapLoaded ( )
152
+ {
153
+ // Compute global line bounds to initialize camera
154
+ LatLngBounds . Builder boundsBuilder = new LatLngBounds . Builder ( ) ;
155
+ foreach ( Route route in line . Routes )
156
+ foreach ( Step step in route . Steps )
157
+ boundsBuilder . Include ( new LatLng ( step . Stop . Position . Latitude , step . Stop . Position . Longitude ) ) ;
158
+
159
+ CameraUpdate cameraUpdate = CameraUpdateFactory . NewLatLngBounds ( boundsBuilder . Build ( ) , 100 ) ;
160
+ googleMap . MoveCamera ( cameraUpdate ) ;
161
+ }
157
162
158
163
public void OnRefreshing ( )
159
164
{
160
165
}
161
166
public void OnRefreshed ( IEnumerable < TimeStep > timeSteps , IEnumerable < Transport > transports )
162
167
{
168
+ List < Transport > unusedTransports = transportMarkers . Keys . ToList ( ) ;
169
+
170
+ foreach ( Transport transport in transports )
171
+ {
172
+ Marker marker ;
173
+
174
+ if ( ! transportMarkers . TryGetValue ( transport , out marker ) )
175
+ {
176
+
177
+ }
178
+ else
179
+ {
180
+ unusedTransports . Remove ( transport ) ;
181
+ }
182
+ }
163
183
}
164
184
}
165
185
}
0 commit comments