Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/fragments/MapFragment.java
Show All 17 Lines | |||||
import androidx.activity.result.ActivityResultCallback; | import androidx.activity.result.ActivityResultCallback; | ||||
import androidx.activity.result.ActivityResultLauncher; | import androidx.activity.result.ActivityResultLauncher; | ||||
import androidx.activity.result.contract.ActivityResultContracts; | import androidx.activity.result.contract.ActivityResultContracts; | ||||
import androidx.annotation.NonNull; | import androidx.annotation.NonNull; | ||||
import androidx.annotation.Nullable; | import androidx.annotation.Nullable; | ||||
import androidx.core.content.res.ResourcesCompat; | import androidx.core.content.res.ResourcesCompat; | ||||
import androidx.preference.PreferenceManager; | import androidx.preference.PreferenceManager; | ||||
import it.reyboz.bustorino.backend.utils; | |||||
import org.osmdroid.api.IGeoPoint; | import org.osmdroid.api.IGeoPoint; | ||||
import org.osmdroid.api.IMapController; | import org.osmdroid.api.IMapController; | ||||
import org.osmdroid.config.Configuration; | import org.osmdroid.config.Configuration; | ||||
import org.osmdroid.events.DelayedMapListener; | import org.osmdroid.events.DelayedMapListener; | ||||
import org.osmdroid.events.MapListener; | import org.osmdroid.events.MapListener; | ||||
import org.osmdroid.events.ScrollEvent; | import org.osmdroid.events.ScrollEvent; | ||||
import org.osmdroid.events.ZoomEvent; | import org.osmdroid.events.ZoomEvent; | ||||
import org.osmdroid.tileprovider.tilesource.TileSourceFactory; | import org.osmdroid.tileprovider.tilesource.TileSourceFactory; | ||||
import org.osmdroid.util.BoundingBox; | import org.osmdroid.util.BoundingBox; | ||||
import org.osmdroid.util.GeoPoint; | import org.osmdroid.util.GeoPoint; | ||||
import org.osmdroid.views.MapView; | import org.osmdroid.views.MapView; | ||||
import org.osmdroid.views.overlay.FolderOverlay; | import org.osmdroid.views.overlay.FolderOverlay; | ||||
import org.osmdroid.views.overlay.Marker; | import org.osmdroid.views.overlay.Marker; | ||||
import org.osmdroid.views.overlay.infowindow.InfoWindow; | import org.osmdroid.views.overlay.infowindow.InfoWindow; | ||||
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider; | import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider; | ||||
import java.lang.ref.WeakReference; | import java.lang.ref.WeakReference; | ||||
import java.util.Arrays; | import java.util.*; | ||||
import java.util.HashSet; | |||||
import java.util.List; | |||||
import java.util.Map; | |||||
import it.reyboz.bustorino.R; | import it.reyboz.bustorino.R; | ||||
import it.reyboz.bustorino.backend.Stop; | import it.reyboz.bustorino.backend.Stop; | ||||
import it.reyboz.bustorino.data.NextGenDB; | import it.reyboz.bustorino.data.NextGenDB; | ||||
import it.reyboz.bustorino.map.CustomInfoWindow; | import it.reyboz.bustorino.map.CustomInfoWindow; | ||||
import it.reyboz.bustorino.map.LocationOverlay; | import it.reyboz.bustorino.map.LocationOverlay; | ||||
import it.reyboz.bustorino.middleware.GeneralActivity; | import it.reyboz.bustorino.middleware.GeneralActivity; | ||||
import it.reyboz.bustorino.util.Permissions; | import it.reyboz.bustorino.util.Permissions; | ||||
Show All 12 Lines | public class MapFragment extends BaseFragment { | ||||
public static final String BUNDLE_ID = "ID"; | public static final String BUNDLE_ID = "ID"; | ||||
public static final String FRAGMENT_TAG="BusTOMapFragment"; | public static final String FRAGMENT_TAG="BusTOMapFragment"; | ||||
private static final double DEFAULT_CENTER_LAT = 45.0708; | private static final double DEFAULT_CENTER_LAT = 45.0708; | ||||
private static final double DEFAULT_CENTER_LON = 7.6858; | private static final double DEFAULT_CENTER_LON = 7.6858; | ||||
private static final double POSITION_FOUND_ZOOM = 18.3; | private static final double POSITION_FOUND_ZOOM = 18.3; | ||||
public static final double NO_POSITION_ZOOM = 17.1; | |||||
private static final String DEBUG_TAG=FRAGMENT_TAG; | private static final String DEBUG_TAG=FRAGMENT_TAG; | ||||
protected FragmentListenerMain listenerMain; | protected FragmentListenerMain listenerMain; | ||||
private HashSet<String> shownStops = null; | private HashSet<String> shownStops = null; | ||||
//the asynctask used to get the stops from the database | //the asynctask used to get the stops from the database | ||||
private AsyncStopFetcher stopFetcher = null; | private AsyncStopFetcher stopFetcher = null; | ||||
▲ Show 20 Lines • Show All 307 Lines • ▼ Show 20 Lines | public void startMap(Bundle incoming, Bundle savedInstanceState) { | ||||
// move the map on the marker position or on a default view point: Turin, Piazza Castello | // move the map on the marker position or on a default view point: Turin, Piazza Castello | ||||
// and set the start zoom | // and set the start zoom | ||||
IMapController mapController = map.getController(); | IMapController mapController = map.getController(); | ||||
GeoPoint startPoint = null; | GeoPoint startPoint = null; | ||||
startLocationOverlay(Permissions.locationPermissionGranted(activity), | startLocationOverlay(Permissions.locationPermissionGranted(activity), | ||||
map); | map); | ||||
// set the center point | // set the center point | ||||
if (marker != null) { | if (marker != null) { | ||||
startPoint = marker; | //startPoint = marker; | ||||
mapController.setZoom(POSITION_FOUND_ZOOM); | mapController.setZoom(POSITION_FOUND_ZOOM); | ||||
setLocationFollowing(false); | setLocationFollowing(false); | ||||
// put the center a little bit off (animate later) | |||||
startPoint = new GeoPoint(marker); | |||||
startPoint.setLatitude(marker.getLatitude()+ utils.angleRawDifferenceFromMeters(20)); | |||||
startPoint.setLongitude(marker.getLongitude()-utils.angleRawDifferenceFromMeters(20)); | |||||
//don't need to do all the rest since we want to show a point | |||||
} else if (savedInstanceState != null && savedInstanceState.containsKey(MAP_CURRENT_ZOOM_KEY)) { | } else if (savedInstanceState != null && savedInstanceState.containsKey(MAP_CURRENT_ZOOM_KEY)) { | ||||
mapController.setZoom(savedInstanceState.getDouble(MAP_CURRENT_ZOOM_KEY)); | mapController.setZoom(savedInstanceState.getDouble(MAP_CURRENT_ZOOM_KEY)); | ||||
mapController.setCenter(new GeoPoint(savedInstanceState.getDouble(MAP_CENTER_LAT_KEY), | mapController.setCenter(new GeoPoint(savedInstanceState.getDouble(MAP_CENTER_LAT_KEY), | ||||
savedInstanceState.getDouble(MAP_CENTER_LON_KEY))); | savedInstanceState.getDouble(MAP_CENTER_LON_KEY))); | ||||
Log.d(DEBUG_TAG, "Location following from savedInstanceState: "+savedInstanceState.getBoolean(FOLLOWING_LOCAT_KEY)); | Log.d(DEBUG_TAG, "Location following from savedInstanceState: "+savedInstanceState.getBoolean(FOLLOWING_LOCAT_KEY)); | ||||
setLocationFollowing(savedInstanceState.getBoolean(FOLLOWING_LOCAT_KEY)); | setLocationFollowing(savedInstanceState.getBoolean(FOLLOWING_LOCAT_KEY)); | ||||
} else { | } else { | ||||
Log.d(DEBUG_TAG, "No position found from intent or saved state"); | Log.d(DEBUG_TAG, "No position found from intent or saved state"); | ||||
Show All 9 Lines | public void startMap(Bundle incoming, Bundle savedInstanceState) { | ||||
mapController.setZoom(POSITION_FOUND_ZOOM); | mapController.setZoom(POSITION_FOUND_ZOOM); | ||||
startPoint = new GeoPoint(userLocation); | startPoint = new GeoPoint(userLocation); | ||||
found = true; | found = true; | ||||
setLocationFollowing(true); | setLocationFollowing(true); | ||||
} | } | ||||
} | } | ||||
if(!found){ | if(!found){ | ||||
startPoint = new GeoPoint(DEFAULT_CENTER_LAT, DEFAULT_CENTER_LON); | startPoint = new GeoPoint(DEFAULT_CENTER_LAT, DEFAULT_CENTER_LON); | ||||
mapController.setZoom(17.0); | mapController.setZoom(NO_POSITION_ZOOM); | ||||
setLocationFollowing(false); | setLocationFollowing(false); | ||||
} | } | ||||
} | } | ||||
// set the minimum zoom level | // set the minimum zoom level | ||||
map.setMinZoomLevel(15.0); | map.setMinZoomLevel(15.0); | ||||
//add contingency check (shouldn't happen..., but) | //add contingency check (shouldn't happen..., but) | ||||
if (startPoint != null) { | if (startPoint != null) { | ||||
mapController.setCenter(startPoint); | mapController.setCenter(startPoint); | ||||
} | } | ||||
//add stops overlay | //add stops overlay | ||||
//map.getOverlays().add(mLocationOverlay); | //map.getOverlays().add(mLocationOverlay); | ||||
map.getOverlays().add(this.stopsFolderOverlay); | map.getOverlays().add(this.stopsFolderOverlay); | ||||
Log.d(DEBUG_TAG, "Requesting stops load"); | Log.d(DEBUG_TAG, "Requesting stops load"); | ||||
// This is not necessary, by setting the center we already move | // This is not necessary, by setting the center we already move | ||||
// the map and we trigger a stop request | // the map and we trigger a stop request | ||||
//requestStopsToShow(); | //requestStopsToShow(); | ||||
if (marker != null) { | if (marker != null) { | ||||
// make a marker with the info window open for the searched marker | // make a marker with the info window open for the searched marker | ||||
makeMarker(startPoint, name , ID, true); | Marker stopMarker = makeMarker(marker, name , ID, true); | ||||
map.getController().animateTo(marker); | |||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Start a request to load the stops that are in the current view | * Start a request to load the stops that are in the current view | ||||
* from the database | * from the database | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | public Marker makeMarker(GeoPoint geoPoint, String stopName, String ID, boolean isStartMarker) { | ||||
// set custom info window as info window | // set custom info window as info window | ||||
CustomInfoWindow popup = new CustomInfoWindow(map, ID, stopName, responder); | CustomInfoWindow popup = new CustomInfoWindow(map, ID, stopName, responder); | ||||
marker.setInfoWindow(popup); | marker.setInfoWindow(popup); | ||||
// make the marker clickable | // make the marker clickable | ||||
marker.setOnMarkerClickListener((thisMarker, mapView) -> { | marker.setOnMarkerClickListener((thisMarker, mapView) -> { | ||||
if (thisMarker.isInfoWindowOpen()) { | if (thisMarker.isInfoWindowOpen()) { | ||||
// on second click | // on second click | ||||
//TODO: show the arrivals for the stop | |||||
Log.w(DEBUG_TAG, "Pressed on the click marker"); | Log.w(DEBUG_TAG, "Pressed on the click marker"); | ||||
} else { | } else { | ||||
// on first click | // on first click | ||||
// hide all opened info window | // hide all opened info window | ||||
InfoWindow.closeAllInfoWindowsOn(map); | InfoWindow.closeAllInfoWindowsOn(map); | ||||
// show this particular info window | // show this particular info window | ||||
thisMarker.showInfoWindow(); | thisMarker.showInfoWindow(); | ||||
Show All 14 Lines | public Marker makeMarker(GeoPoint geoPoint, String stopName, String ID, boolean isStartMarker) { | ||||
// add to it a title | // add to it a title | ||||
marker.setTitle(stopName); | marker.setTitle(stopName); | ||||
// set the description as the ID | // set the description as the ID | ||||
marker.setSnippet(ID); | marker.setSnippet(ID); | ||||
// show popup info window of the searched marker | // show popup info window of the searched marker | ||||
if (isStartMarker) { | if (isStartMarker) { | ||||
marker.showInfoWindow(); | marker.showInfoWindow(); | ||||
//map.getController().animateTo(marker.getPosition()); | |||||
} | } | ||||
return marker; | return marker; | ||||
} | } | ||||
/** | /** | ||||
* Simple asyncTask class to load the stops in the background | * Simple asyncTask class to load the stops in the background | ||||
* Holds a weak reference to the fragment to do callbacks | * Holds a weak reference to the fragment to do callbacks | ||||
▲ Show 20 Lines • Show All 52 Lines • Show Last 20 Lines |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register