Skip to content

Commit d511c0e

Browse files
committed
Fixed some bugs and added one more tile source
1 parent 3ae7efd commit d511c0e

File tree

7 files changed

+190
-38
lines changed

7 files changed

+190
-38
lines changed

app/src/main/java/kevin/carrismobile/data/Carreira.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public void init(){
6666
}
6767
//TODO not sure how it impacts performance
6868
for(Direction direction : directionList){
69+
direction.setPointList(Api.getPoints(direction.getShape_id()));
6970
for (Path path : direction.getPathList()){
7071
path.getStop().setOnline(true);
7172
path.getStop().setAgency_id("-1");

app/src/main/java/kevin/carrismobile/data/Direction.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class Direction implements Serializable {
1313
private String id;
1414
private String headsign;
1515
private String shape_id;
16+
List<Point> pointList;
1617
private List<Path> path = new ArrayList<>();
1718
private List<Trip> trips = new ArrayList<>();
1819

@@ -50,6 +51,13 @@ public void setHeuristic(double heuristic) {
5051
this.heuristic = heuristic;
5152
}
5253
}
54+
public List<Point> getPointList() {
55+
return pointList;
56+
}
57+
58+
public void setPointList(List<Point> pointList) {
59+
this.pointList = pointList;
60+
}
5361

5462
public String getDirectionId() {
5563
return id;

app/src/main/java/kevin/carrismobile/fragments/RealTimeFragment.java

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.app.AlertDialog;
1111
import android.graphics.Bitmap;
1212
import android.graphics.Canvas;
13+
import android.graphics.Color;
1314
import android.graphics.drawable.BitmapDrawable;
1415
import android.graphics.drawable.Drawable;
1516
import android.graphics.drawable.LayerDrawable;
@@ -33,17 +34,21 @@
3334
import org.osmdroid.views.CustomZoomButtonsController;
3435
import org.osmdroid.views.MapView;
3536
import org.osmdroid.views.overlay.Marker;
37+
import org.osmdroid.views.overlay.Polyline;
3638
import org.osmdroid.views.overlay.compass.CompassOverlay;
3739
import org.osmdroid.views.overlay.infowindow.MarkerInfoWindow;
3840

3941
import java.util.ArrayList;
4042
import java.util.List;
4143

44+
import kevin.carrismobile.adaptors.StopImageListAdaptor;
4245
import kevin.carrismobile.api.Api;
4346
import kevin.carrismobile.data.Bus;
4447
import kevin.carrismobile.data.Carreira;
4548
import kevin.carrismobile.data.Direction;
4649
import kevin.carrismobile.data.Path;
50+
import kevin.carrismobile.data.Point;
51+
import kevin.carrismobile.data.Stop;
4752
import kevin.carrismobile.gui.BusBackgroundThread;
4853
import kevin.carrismobile.gui.CustomMarkerInfoWindow;
4954
import 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() + "\nLocality: " + path.getStop().getLocality() + "\nMunicipality: " + 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() + "\nLocality: " + s.getLocality() + "\nMunicipality: " + 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

app/src/main/java/kevin/carrismobile/fragments/RouteDetailsFragment.java

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public class RouteDetailsFragment extends Fragment {
9191
public AlertDialog routeDeleted;
9292
public boolean connected;
9393
public List<Marker> markerList = new ArrayList<>();//
94+
public Polyline line = null;
9495

9596

9697
@Override
@@ -545,6 +546,7 @@ private void updateMarkers(List<Stop> stopList, MapView map){
545546
for (Marker marker : markerList){
546547
map.getOverlays().remove(marker);
547548
}
549+
markerList.clear();
548550
List<GeoPoint> geoPointList = new ArrayList<>();
549551
for (Stop s : stopList){
550552
double[] coordinates = s.getCoordinates();
@@ -560,30 +562,27 @@ private void updateMarkers(List<Stop> stopList, MapView map){
560562
MarkerInfoWindow miw= new CustomMarkerInfoWindow(org.osmdroid.library.R.layout.bonuspack_bubble, map, s.getTts_name(), descrption);
561563
marker.setInfoWindow(miw);
562564
}
563-
Polyline line = new Polyline(map, true, false);
564-
new Thread(new Runnable() {
565-
@Override
566-
public void run() {
567-
if (currentCarreira.isOnline()){
568-
Direction currentDirection = currentCarreira.getDirectionList().get(currentDirectionIndex);
569-
Log.d("DEBUG", currentDirection.getShape_id());
570-
List<Point> pointList = Api.getPoints(currentDirection.getShape_id());
571-
pointList.forEach(point -> line.addPoint(new GeoPoint(point.getLat(), point.getLon())));
572-
String hexCode = currentCarreira.getColor().substring(1);
573-
int resultRed = Integer.valueOf(hexCode.substring(0, 2), 16);
574-
int resultGreen = Integer.valueOf(hexCode.substring(2, 4), 16);
575-
int resultBlue = Integer.valueOf(hexCode.substring(4, 6), 16);
576-
line.setColor(Color.rgb(resultRed, resultGreen, resultBlue));
577-
line.setWidth(7.5f);
578-
map.getOverlays().add(line);
579-
markerList.forEach(marker -> map.getOverlays().add(marker));
580-
}else{
581-
geoPointList.forEach(line::addPoint);
582-
map.getOverlays().add(line);
583-
markerList.forEach(marker -> map.getOverlays().add(marker));
584-
}
585-
}
586-
}).start();
565+
if (line != null){
566+
map.getOverlays().remove(line);
567+
}
568+
line = new Polyline(map, true, false);
569+
if (currentCarreira.isOnline()){
570+
Direction currentDirection = currentCarreira.getDirectionList().get(currentDirectionIndex);
571+
List<Point> pointList = currentDirection.getPointList();
572+
pointList.forEach(point -> line.addPoint(new GeoPoint(point.getLat(), point.getLon())));
573+
String hexCode = currentCarreira.getColor().substring(1);
574+
int resultRed = Integer.valueOf(hexCode.substring(0, 2), 16);
575+
int resultGreen = Integer.valueOf(hexCode.substring(2, 4), 16);
576+
int resultBlue = Integer.valueOf(hexCode.substring(4, 6), 16);
577+
line.setColor(Color.rgb(resultRed, resultGreen, resultBlue));
578+
line.setWidth(7.5f);
579+
map.getOverlays().add(line);
580+
markerList.forEach(marker -> map.getOverlays().add(marker));
581+
}else{
582+
geoPointList.forEach(line::addPoint);
583+
map.getOverlays().add(line);
584+
markerList.forEach(marker -> map.getOverlays().add(marker));
585+
}
587586
}
588587
public String getCurrentCarreiraId() {
589588
return currentCarreiraId;

app/src/main/java/kevin/carrismobile/fragments/SettingsFragment.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.Objects;
3434

3535
import kevin.carrismobile.custom.MyCustomDialog;
36+
import kevin.carrismobile.tile_source.MyMapTilerTileSource;
3637
import kevin.carrismobile.tile_source.MyThunderForestTileSource;
3738

3839

@@ -42,6 +43,7 @@ public class SettingsFragment extends Fragment {
4243
public Switch openTopoSwitch;
4344
public Switch thunderForestSwitch;
4445
public Switch bingMapsSwitch;
46+
public Switch mapTilerSwitch;
4547
public Button resetButton;
4648

4749
@Override
@@ -51,6 +53,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
5153
openTopoSwitch = v.findViewById(R.id.openTopoSwitch);
5254
thunderForestSwitch = v.findViewById(R.id.thunderForestSwitch);
5355
bingMapsSwitch = v.findViewById(R.id.bingMapsSwitch);
56+
mapTilerSwitch = v.findViewById(R.id.mapTilerSwitch);
5457
resetButton = v.findViewById(R.id.resetPreferences);
5558
mPrefs = getActivity().getSharedPreferences("SettingsFragment", MODE_PRIVATE);
5659
if (loadObject("key_topo", String.class) == null){
@@ -71,6 +74,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
7174
setOpenTopoSwitchOnClickListener();
7275
setBingMapsSwitchOnClickListener();
7376
setThunderForestSwitchOnClickListener();
77+
setMapTilerSwitchOnClickListener();
7478
resetButton.setOnClickListener(new View.OnClickListener() {
7579
@Override
7680
public void onClick(View view) {
@@ -95,6 +99,8 @@ public void onClick(View view) {
9599
thunderForestSwitch.setChecked(false);
96100
}else if(bingMapsSwitch.isChecked()){
97101
bingMapsSwitch.setChecked(false);
102+
}else if(mapTilerSwitch.isChecked()){
103+
mapTilerSwitch.setChecked(false);
98104
}else if(!openTopoSwitch.isChecked()){
99105
openTopoSwitch.setChecked(true);
100106
}
@@ -120,6 +126,8 @@ public void onClick(View view) {
120126
openTopoSwitch.setChecked(false);
121127
}else if(thunderForestSwitch.isChecked()){
122128
thunderForestSwitch.setChecked(false);
129+
}else if(mapTilerSwitch.isChecked()){
130+
mapTilerSwitch.setChecked(false);
123131
}else if(!bingMapsSwitch.isChecked()){
124132
bingMapsSwitch.setChecked(true);
125133
}
@@ -155,6 +163,8 @@ public void onClick(View view) {
155163
openTopoSwitch.setChecked(false);
156164
}else if(bingMapsSwitch.isChecked()){
157165
bingMapsSwitch.setChecked(false);
166+
}else if(mapTilerSwitch.isChecked()){
167+
mapTilerSwitch.setChecked(false);
158168
}else if(!thunderForestSwitch.isChecked()){
159169
thunderForestSwitch.setChecked(true);
160170
}
@@ -193,6 +203,42 @@ private void showEditTextDialog(String key, String tileSourceName){
193203
}).setNegativeButton("Close", (dialogInterface, i) -> dialogInterface.dismiss()).create();
194204
alertDialog.show();
195205
}
206+
private void setMapTilerSwitchOnClickListener(){
207+
mapTilerSwitch.setOnClickListener(new View.OnClickListener() {
208+
@Override
209+
public void onClick(View view) {
210+
if (openTopoSwitch.isChecked()){
211+
openTopoSwitch.setChecked(false);
212+
}else if(bingMapsSwitch.isChecked()){
213+
bingMapsSwitch.setChecked(false);
214+
}else if(thunderForestSwitch.isChecked()){
215+
thunderForestSwitch.setChecked(false);
216+
}else if(!mapTilerSwitch.isChecked()){
217+
mapTilerSwitch.setChecked(true);
218+
}
219+
MainActivity activity = (MainActivity) getActivity();
220+
MapView map1 = ((StopsMapFragment) activity.stopsMapFragment).getMap();
221+
MapView map2 = ((RealTimeFragment) activity.realTimeFragment).getMap();
222+
MapView map3 = ((RouteDetailsFragment) activity.routeDetailsFragment).getMap();
223+
224+
MyMapTilerTileSource mapTilerSource = new MyMapTilerTileSource();
225+
String apiKey = (String)loadObject("key_apiKey_mapTiler", String.class);
226+
if (apiKey == null){
227+
showEditTextDialog("key_apiKey_mapTiler", "Map Tiler");
228+
apiKey = (String)loadObject("key_apiKey_mapTiler", String.class);
229+
}
230+
mapTilerSource.setApiKey(apiKey);
231+
232+
map1.setTileSource(mapTilerSource);
233+
map2.setTileSource(mapTilerSource);
234+
map3.setTileSource(mapTilerSource);
235+
map1.invalidate();
236+
map2.invalidate();
237+
map3.invalidate();
238+
storeObject(new Gson().toJson("1"), "key_mapTiler");
239+
}
240+
});
241+
}
196242
private void storeObject(String json, String key){
197243

198244
Thread thread = new Thread(new Runnable() {
Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
11
package kevin.carrismobile.tile_source;
22

3+
import android.util.Log;
4+
35
import org.osmdroid.tileprovider.tilesource.OnlineTileSourceBase;
46
import org.osmdroid.tileprovider.tilesource.XYTileSource;
7+
import org.osmdroid.util.MapTileIndex;
8+
9+
public class MyMapTilerTileSource extends OnlineTileSourceBase {
10+
private String apiKey = "";
11+
public static String[] baseUrl = new String[]{"https://api.maptiler.com/maps/openstreetmap/256/"};
12+
13+
public MyMapTilerTileSource() {
14+
super("OpenStreetMap", 0, 18, 256, ".jpg", baseUrl, "Maps © MapTiler, Data © OpenStreetMap contributors.");
15+
//this line will ensure uniqueness in the tile cache
16+
//mName="thunderforest"+aMap+mMapId;
17+
}
518

6-
public class MyMapTilerTileSource {
19+
public void setApiKey(String apiKey) {
20+
this.apiKey = apiKey;
21+
}
722

8-
public static final OnlineTileSourceBase Pastel = new XYTileSource("OpenStreetMap", 1, 22, 256, ".png",
9-
new String[]{
10-
"https://api.maptiler.com/maps/basic/?key=pXCee81XdHjf2bL2W9ev"});
23+
public String getApiKey() {
24+
return apiKey;
25+
}
1126

27+
@Override
28+
public String getTileURLString(long pMapTileIndex) {
29+
StringBuilder url = new StringBuilder();
30+
url.append(baseUrl[0]);
31+
url.append(MapTileIndex.getZoom(pMapTileIndex));
32+
url.append("/");
33+
url.append(MapTileIndex.getX(pMapTileIndex));
34+
url.append("/");
35+
url.append(MapTileIndex.getY(pMapTileIndex));
36+
url.append(".jpg?");
37+
url.append("key=").append(apiKey);
38+
String res = url.toString();
39+
return res;
40+
}
1241
}

0 commit comments

Comments
 (0)