1010import android .app .AlertDialog ;
1111import android .graphics .Bitmap ;
1212import android .graphics .Canvas ;
13+ import android .graphics .Color ;
1314import android .graphics .drawable .BitmapDrawable ;
1415import android .graphics .drawable .Drawable ;
1516import android .graphics .drawable .LayerDrawable ;
3334import org .osmdroid .views .CustomZoomButtonsController ;
3435import org .osmdroid .views .MapView ;
3536import org .osmdroid .views .overlay .Marker ;
37+ import org .osmdroid .views .overlay .Polyline ;
3638import org .osmdroid .views .overlay .compass .CompassOverlay ;
3739import org .osmdroid .views .overlay .infowindow .MarkerInfoWindow ;
3840
3941import java .util .ArrayList ;
4042import java .util .List ;
4143
44+ import kevin .carrismobile .adaptors .StopImageListAdaptor ;
4245import kevin .carrismobile .api .Api ;
4346import kevin .carrismobile .data .Bus ;
4447import kevin .carrismobile .data .Carreira ;
4548import kevin .carrismobile .data .Direction ;
4649import kevin .carrismobile .data .Path ;
50+ import kevin .carrismobile .data .Point ;
51+ import kevin .carrismobile .data .Stop ;
4752import kevin .carrismobile .gui .BusBackgroundThread ;
4853import kevin .carrismobile .gui .CustomMarkerInfoWindow ;
4954import kevin .carrismobile .custom .MyCustomDialog ;
@@ -67,6 +72,7 @@ public class RealTimeFragment extends Fragment {
6772 public static int currentSelectedBus = 0 ;
6873 public int currentDirectionIndex = 0 ;
6974 public boolean connected = false ;
75+ public Polyline line = null ;
7076 public AlertDialog dialog ;
7177 public AlertDialog backgroundDialog ;
7278 public AlertDialog noCurrentBusesDialog ;
@@ -174,9 +180,10 @@ public void run() {
174180 getActivity ().runOnUiThread (new Runnable () {
175181 @ Override
176182 public void run () {
177- updateMarkers (pathList , map , getActivity () );
183+ updateMarkers (pathList , map );
178184 updateBuses (busList , map , getActivity ());
179185 updateTextView ();
186+ updateDirectionIndex ();
180187 Log .println (Log .DEBUG , "BUS DEBUG" , "GUI UPDATED" );
181188 GeoPoint point = markerBusList .get (currentSelectedBus ).getPosition ();
182189 map .getController ().animateTo (point , 16.5 , 1500L );
@@ -206,6 +213,8 @@ public void onClick(View view) {
206213 if (currentSelectedBus < busList .size () - 1 ){
207214 currentSelectedBus ++;
208215 updateTextView ();
216+ updateDirectionIndex ();
217+ updateMarkers (currentCarreira .getDirectionList ().get (currentDirectionIndex ).getPathList (), map );
209218 Log .println (Log .DEBUG , "Button" , "Current Bus: " + currentSelectedBus );
210219 getActivity ().runOnUiThread (new Runnable () {
211220
@@ -232,6 +241,8 @@ public void onClick(View view) {
232241 if (currentSelectedBus > 0 ){
233242 currentSelectedBus --;
234243 updateTextView ();
244+ updateDirectionIndex ();
245+ updateMarkers (currentCarreira .getDirectionList ().get (currentDirectionIndex ).getPathList (), map );
235246 Log .println (Log .DEBUG , "Button" , "Current Bus: " + currentSelectedBus );
236247 getActivity ().runOnUiThread (new Runnable () {
237248 @ Override
@@ -250,24 +261,58 @@ public void run() {
250261 return v ;
251262 }
252263
253- private static void updateMarkers (List <Path > pathList , MapView map , Activity activity ){
264+ private void updateDirectionIndex () {
265+ int newIndex = 0 ;
266+ String directionId = busList .get (currentSelectedBus ).getPattern_id ();
267+ for (Direction direction : currentCarreira .getDirectionList ()){
268+ if (direction .getDirectionId ().equals (directionId )){
269+ currentDirectionIndex = newIndex ;
270+ }
271+ newIndex ++;
272+ }
273+ }
274+
275+ private void updateMarkers (List <Path > pathList , MapView map ){
254276 for (Marker marker : markerList ){
255277 map .getOverlays ().remove (marker );
256278 }
257-
279+ markerList .clear ();
280+ List <GeoPoint > geoPointList = new ArrayList <>();
258281 for (Path path : pathList ){
259- double [] coordinates = path .getStop ().getCoordinates ();
282+ Stop s = path .getStop ();
283+ double [] coordinates = s .getCoordinates ();
260284 GeoPoint point = new GeoPoint (coordinates [0 ], coordinates [1 ]);
285+ geoPointList .add (point );
261286 Marker marker = new Marker (map );
262- Drawable d = ResourcesCompat . getDrawable ( activity . getResources (), R . drawable . map_pin , null );
287+ Drawable d = StopImageListAdaptor . getImageId ( s . getFacilities (), s . getTts_name (), s . getAgency_id (), getActivity () );
263288 marker .setIcon (d );
264289 markerList .add (marker );
265290 marker .setPosition (point );
266291 marker .setAnchor (Marker .ANCHOR_CENTER , Marker .ANCHOR_CENTER );
267- String descrption = "Stop Id: " + path . getStop (). getStopID () + "\n Locality: " + path . getStop (). getLocality () + "\n Municipality: " + path . getStop () .getMunicipality_name ();
268- MarkerInfoWindow miw = new CustomMarkerInfoWindow (org .osmdroid .library .R .layout .bonuspack_bubble , map , path . getStop () .getTts_name (), descrption );
292+ String descrption = "Stop Id: " + s . getStopID () + "\n Locality: " + s . getLocality () + "\n Municipality: " + s .getMunicipality_name ();
293+ MarkerInfoWindow miw = new CustomMarkerInfoWindow (org .osmdroid .library .R .layout .bonuspack_bubble , map , s .getTts_name (), descrption );
269294 marker .setInfoWindow (miw );
270- map .getOverlays ().add (marker );
295+ }
296+ if (line != null ){
297+ map .getOverlays ().remove (line );
298+ }
299+ line = new Polyline (map , true , false );
300+ if (currentCarreira .isOnline ()){
301+ Direction currentDirection = currentCarreira .getDirectionList ().get (currentDirectionIndex );
302+ List <Point > pointList = currentDirection .getPointList ();
303+ pointList .forEach (point -> line .addPoint (new GeoPoint (point .getLat (), point .getLon ())));
304+ String hexCode = currentCarreira .getColor ().substring (1 );
305+ int resultRed = Integer .valueOf (hexCode .substring (0 , 2 ), 16 );
306+ int resultGreen = Integer .valueOf (hexCode .substring (2 , 4 ), 16 );
307+ int resultBlue = Integer .valueOf (hexCode .substring (4 , 6 ), 16 );
308+ line .setColor (Color .rgb (resultRed , resultGreen , resultBlue ));
309+ line .setWidth (7.5f );
310+ map .getOverlays ().add (line );
311+ markerList .forEach (marker -> map .getOverlays ().add (marker ));
312+ }else {
313+ geoPointList .forEach (line ::addPoint );
314+ map .getOverlays ().add (line );
315+ markerList .forEach (marker -> map .getOverlays ().add (marker ));
271316 }
272317 }
273318
0 commit comments