use of cl.smartcities.isci.transportinspector.backend.BusStop in project androidApp by InspectorIncognito.
the class BusStopPlugin method setupData.
private void setupData() {
if (mapboxMap == null) {
return;
}
GridHelper helper = new GridHelper(getContext());
List<BusStop> busStops = helper.getNearBusStops(mapboxMap.getCameraPosition().target.getLatitude(), mapboxMap.getCameraPosition().target.getLongitude());
helper.close();
onBusEngine.setBusStops(busStops);
List<Feature> markerCoordinates = new ArrayList<>();
for (BusStop busStop : busStops) {
markerCoordinates.add(new BusStopMarker(busStop));
}
busStopFeatures = FeatureCollection.fromFeatures(markerCoordinates);
busesFeatures = FeatureCollection.fromFeatures(new Feature[] {});
busRouteStartFeature = FeatureCollection.fromFeatures(new Feature[] {});
busRouteEndFeature = FeatureCollection.fromFeatures(new Feature[] {});
setupBusRouteSource();
setupBusRouteLayer();
setupBusStopSource();
setupBusStopLayer();
setupBusSource();
setupBusLayer();
}
use of cl.smartcities.isci.transportinspector.backend.BusStop in project androidApp by InspectorIncognito.
the class BusStopPlugin method busStopFeatureClick.
private void busStopFeatureClick(Feature busStopFeature, BusStopClickHandler handler) {
if (selectedBus != null) {
Log.d(BusStopPlugin.class.getSimpleName(), "selectedBus != null");
deselectSelectedBusFeature();
}
final Feature newSelectedFeature = handler.handleMarkerClick(busStopFeature, busStopFeatures);
if (newSelectedFeature != null) {
Log.d(BusStopPlugin.class.getSimpleName(), "newSelectedFeature != null");
if (busStopServiceCall != null && !busStopServiceCall.isCanceled()) {
busStopServiceCall.cancel();
}
busesFeatures = FeatureCollection.fromFeatures(new Feature[] {});
if (periodicRequestHandler != null) {
periodicRequestHandler.stopPeriodicRequest();
periodicRequestHandler = null;
}
refreshBusSource();
Log.d(BusStopPlugin.class.getSimpleName(), "selectedBusStop assign");
if (selectedBusStop != null) {
deselectSelectedBusStop();
}
selectedBusStop = newSelectedFeature;
final BusStop busStop = ((BusStopMarker) newSelectedFeature).getBusStop();
final BusStopLoadingLayerTask loaderTask = new BusStopLoadingLayerTask(newSelectedFeature, new BusStopLoadingLayerTask.LoadingListener() {
@Override
public void refresh() {
refreshBusStopSource();
}
});
final Thread thread = new Thread(loaderTask);
thread.start();
busStopServiceCall = busStopInfoService.getBusStopData(Installation.getPhoneId(), newSelectedFeature.getStringProperty(BusStopMarker.PROPERTY_ID));
/*Log the URL called*/
Log.d(BusStopPlugin.class.getSimpleName(), busStopServiceCall.request().url().toString());
busStopServiceCall.enqueue(new Callback<ServerSentBusStopInfo>() {
@Override
public void onResponse(Call<ServerSentBusStopInfo> call, Response<ServerSentBusStopInfo> response) {
Log.d(BusStopPlugin.class.getSimpleName(), "onResponse");
if (response == null || response.body() == null) {
if (response != null)
Log.e("BusStopPlugin", response.toString());
return;
}
periodicRequestHandler = new PeriodicRequestHandler(getContext(), BusStopPlugin.this, busStop, response.body().getEventsList());
BusStopPlugin.this.onResponse(response.body().getBusList(), response.body().getEventsList(), loaderTask, thread);
}
@Override
public void onFailure(Call<ServerSentBusStopInfo> call, Throwable t) {
Log.d(BusStopPlugin.class.getSimpleName(), "onFailure");
if (!call.isCanceled()) {
Toast.makeText(getContext(), "Error de conexión", Toast.LENGTH_SHORT).show();
}
loaderTask.stop();
thread.interrupt();
deselectSelectedBusStop(newSelectedFeature);
}
});
listener.onBusStopFocusGained();
animateCameraToSelection(newSelectedFeature);
} else {
refreshBusSource();
centerCameraInSelection(busStopFeature);
}
refreshBusStopSource();
}
use of cl.smartcities.isci.transportinspector.backend.BusStop in project androidApp by InspectorIncognito.
the class BusStopHelper method getNearBusStops.
/**
* Open the database, does a query, close the database and return a list of BusStop that
* contains all the near bus stops in a square
*
* @param pLat The latitude from the point where we want to get near bus stops
* @param pLon The longitude from the point where we want to get near bus stops
* @param pRadius The radius of the square (from center to side)
* @return A list of BusStops containing the BusStops in the square
*/
private List<BusStop> getNearBusStops(double pLat, double pLon, double pRadius) {
String query = "SELECT * FROM " + DataBaseContract.Stop.TABLE_NAME + " WHERE " + DataBaseContract.Stop.LATITUDE + " > ? AND " + DataBaseContract.Stop.LATITUDE + " < ? AND " + DataBaseContract.Stop.LONGITUDE + " > ? AND " + DataBaseContract.Stop.LONGITUDE + " < ?";
double latMin = this.destinationPoint(pLat, pLon, pRadius, 0)[0], latMax = this.destinationPoint(pLat, pLon, pRadius, 180)[0], lonMin = this.destinationPoint(pLat, pLon, pRadius, 90)[1], lonMax = this.destinationPoint(pLat, pLon, pRadius, -90)[1];
String[] params = new String[] { Double.toString(latMin), Double.toString(latMax), Double.toString(lonMin), Double.toString(lonMax) };
Cursor cursor = this.getReadableDatabase().rawQuery(query, params);
List<BusStop> busStopList = this.cursorToBusStopList(cursor);
this.close();
return busStopList;
}
use of cl.smartcities.isci.transportinspector.backend.BusStop in project androidApp by InspectorIncognito.
the class BusStopHelper method getNearestBusStops.
/**
* Obtains the top N nearest BusStop in 1km radius, sorted by the distance from the given
* starting point. If there are no BusStops in the radius, returns an empty list.
*
* @param pLat Latitude from the starting point
* @param pLon Longitude from the staring point
* @param pN Indicates the amount of desired BusStops
* @return A list of BusStops containing the top N nearest BusStops
*/
public List<BusStop> getNearestBusStops(double pLat, double pLon, int pN) {
// Query to obtain the BusStops inside a 1km radius
// 1km
double radius = 1000;
List<BusStop> nearBusStops = this.getNearBusStops(pLat, pLon, radius);
// From the obtained BusStops, create a TreeMap
TreeMap<Float, BusStop> treeMap = new TreeMap<>();
for (BusStop busStop : nearBusStops) {
// float[] distance = new float[3];
// Location.distanceBetween(pLat, pLon, busStop.getLat(), busStop.getLon(), distance);
// treeMap.put(distance[0],busStop);
float distance = busStop.getDistance(pLat, pLon);
treeMap.put(distance, busStop);
}
// From the TreeMap we get the pN closest BusStops
List<BusStop> nearestBusStops = new ArrayList<>();
int count = 0;
for (Float key : treeMap.keySet()) {
if (count < pN) {
nearestBusStops.add(treeMap.get(key));
count++;
} else
break;
}
return nearestBusStops;
}
use of cl.smartcities.isci.transportinspector.backend.BusStop in project androidApp by InspectorIncognito.
the class BusStopHelper method reorderBusStops.
private List<BusStop> reorderBusStops(String[] originalOrder, List<BusStop> busStops) {
List<BusStop> result = new ArrayList<>();
for (String code : originalOrder) {
int index = busStops.indexOf(new BusStop(code));
if (index == -1) {
Log.e("ERROR", code);
} else {
result.add(busStops.get(index));
busStops.remove(index);
}
}
return result;
}
Aggregations