Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/fragments/MapFragment.java
Context not available. | |||||
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 | |||||
private AsyncStopFetcher stopFetcher = null; | |||||
private MapView map = null; | private MapView map = null; | ||||
Context not available. | |||||
if(result.get(Manifest.permission.ACCESS_COARSE_LOCATION) && result.get(Manifest.permission.ACCESS_FINE_LOCATION)){ | if(result.get(Manifest.permission.ACCESS_COARSE_LOCATION) && result.get(Manifest.permission.ACCESS_FINE_LOCATION)){ | ||||
map.getOverlays().remove(mLocationOverlay); | map.getOverlays().remove(mLocationOverlay); | ||||
startLocationOverlay(true); | startLocationOverlay(true, map); | ||||
if(getContext()==null || getContext().getSystemService(Context.LOCATION_SERVICE)==null) | if(getContext()==null || getContext().getSystemService(Context.LOCATION_SERVICE)==null) | ||||
return; | return; | ||||
LocationManager locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE); | LocationManager locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE); | ||||
Context not available. | |||||
return fragment; | return fragment; | ||||
} | } | ||||
public static MapFragment getInstance(Stop stop){ | public static MapFragment getInstance(@NonNull Stop stop){ | ||||
return getInstance(stop.getLatitude(), stop.getLongitude(), stop.getStopDisplayName(), stop.ID); | return getInstance(stop.getLatitude(), stop.getLongitude(), stop.getStopDisplayName(), stop.ID); | ||||
} | } | ||||
Context not available. | |||||
public void onPause() { | public void onPause() { | ||||
super.onPause(); | super.onPause(); | ||||
saveMapState(); | saveMapState(); | ||||
//cancel asynctask | |||||
Log.w(DEBUG_TAG, "On pause called"); | |||||
if (stopFetcher!= null) | |||||
stopFetcher.cancel(true); | |||||
} | } | ||||
/** | /** | ||||
Context not available. | |||||
*/ | */ | ||||
public void setLocationFollowing(Boolean value){ | public void setLocationFollowing(Boolean value){ | ||||
followingLocation = value; | followingLocation = value; | ||||
if(mLocationOverlay==null || getContext() == null) | if(mLocationOverlay==null || getContext() == null || map ==null) | ||||
//nothing else to do | //nothing else to do | ||||
return; | return; | ||||
if (value){ | if (value){ | ||||
Context not available. | |||||
} | } | ||||
/** | /** | ||||
* Start the location overlay. Enable only when | * Build the location overlay. Enable only when | ||||
* a) we know we have the permission | * a) we know we have the permission | ||||
* b) the location map is set | * b) the location map is set | ||||
*/ | */ | ||||
private void startLocationOverlay(boolean enableLocation){ | private void startLocationOverlay(boolean enableLocation, MapView map){ | ||||
if(getActivity()== null) throw new IllegalStateException("Cannot enable LocationOverlay now"); | if(getActivity()== null) throw new IllegalStateException("Cannot enable LocationOverlay now"); | ||||
// Location Overlay | // Location Overlay | ||||
// from OpenBikeSharing (THANK GOD) | // from OpenBikeSharing (THANK GOD) | ||||
Context not available. | |||||
GpsMyLocationProvider imlp = new GpsMyLocationProvider(getActivity().getBaseContext()); | GpsMyLocationProvider imlp = new GpsMyLocationProvider(getActivity().getBaseContext()); | ||||
imlp.setLocationUpdateMinDistance(5); | imlp.setLocationUpdateMinDistance(5); | ||||
imlp.setLocationUpdateMinTime(2000); | imlp.setLocationUpdateMinTime(2000); | ||||
this.mLocationOverlay = new LocationOverlay(imlp,map, locationCallbacks); | |||||
if (enableLocation) mLocationOverlay.enableMyLocation(); | |||||
mLocationOverlay.setOptionsMenuEnabled(true); | |||||
map.getOverlays().add(this.mLocationOverlay); | final LocationOverlay overlay = new LocationOverlay(imlp,map, locationCallbacks); | ||||
if (enableLocation) overlay.enableMyLocation(); | |||||
overlay.setOptionsMenuEnabled(true); | |||||
//map.getOverlays().add(this.mLocationOverlay); | |||||
this.mLocationOverlay = overlay; | |||||
map.getOverlays().add(mLocationOverlay); | |||||
} | } | ||||
public void startMap(Bundle incoming, Bundle savedInstanceState) { | public void startMap(Bundle incoming, Bundle savedInstanceState) { | ||||
Context not available. | |||||
}else{ | }else{ | ||||
Log.d(DEBUG_TAG, "Starting map from scratch"); | Log.d(DEBUG_TAG, "Starting map from scratch"); | ||||
} | } | ||||
//clear previous overlays | |||||
map.getOverlays().clear(); | |||||
//parse incoming bundle | //parse incoming bundle | ||||
Context not available. | |||||
ID = incoming.getString(BUNDLE_ID); | ID = incoming.getString(BUNDLE_ID); | ||||
} | } | ||||
//ask for location permission | |||||
if(!Permissions.locationPermissionGranted(activity)){ | |||||
if(shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)){ | |||||
//TODO: show dialog for permission rationale | |||||
Toast.makeText(activity, R.string.enable_position_message_map, Toast.LENGTH_SHORT).show(); | |||||
} | |||||
positionRequestLauncher.launch(Permissions.LOCATION_PERMISSIONS); | |||||
} | |||||
shownStops = new HashSet<>(); | shownStops = new HashSet<>(); | ||||
// 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), | |||||
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); | ||||
} else if (savedInstanceState != null) { | } 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))); | ||||
Context not available. | |||||
found = true; | found = true; | ||||
setLocationFollowing(true); | setLocationFollowing(true); | ||||
} | } | ||||
} else if(!Permissions.locationPermissionGranted(activity)){ | |||||
if(shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)){ | |||||
//TODO: show dialog for permission rationale | |||||
Toast.makeText(activity, R.string.enable_position_message_map, Toast.LENGTH_SHORT).show(); | |||||
} | |||||
positionRequestLauncher.launch(Permissions.LOCATION_PERMISSIONS); | |||||
} | } | ||||
if(!found){ | if(!found){ | ||||
startPoint = new GeoPoint(DEFAULT_CENTER_LAT, DEFAULT_CENTER_LON); | startPoint = new GeoPoint(DEFAULT_CENTER_LAT, DEFAULT_CENTER_LON); | ||||
Context not available. | |||||
setLocationFollowing(false); | setLocationFollowing(false); | ||||
} | } | ||||
} | } | ||||
startLocationOverlay(Permissions.locationPermissionGranted(activity)); | |||||
// set the minimum zoom level | // set the minimum zoom level | ||||
map.setMinZoomLevel(15.0); | map.setMinZoomLevel(15.0); | ||||
Context not available. | |||||
mapController.setCenter(startPoint); | mapController.setCenter(startPoint); | ||||
} | } | ||||
//add stops overlay | //add stops overlay | ||||
//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"); | ||||
Context not available. | |||||
double latTo = bb.getLatNorth(); | double latTo = bb.getLatNorth(); | ||||
double lngFrom = bb.getLonWest(); | double lngFrom = bb.getLonWest(); | ||||
double lngTo = bb.getLonEast(); | double lngTo = bb.getLonEast(); | ||||
if (stopFetcher!= null && stopFetcher.getStatus()!= AsyncTask.Status.FINISHED) | |||||
new AsyncStopFetcher(this).execute( | stopFetcher.cancel(true); | ||||
stopFetcher = new AsyncStopFetcher(this); | |||||
stopFetcher.execute( | |||||
new AsyncStopFetcher.BoundingBoxLimit(lngFrom,lngTo,latFrom, latTo)); | new AsyncStopFetcher.BoundingBoxLimit(lngFrom,lngTo,latFrom, latTo)); | ||||
} | } | ||||
Context not available. | |||||
* @param stops the list of stops that must be included | * @param stops the list of stops that must be included | ||||
*/ | */ | ||||
protected void showStopsMarkers(List<Stop> stops){ | protected void showStopsMarkers(List<Stop> stops){ | ||||
if (getContext() == null){ | |||||
//we are not attached | |||||
return; | |||||
} | |||||
boolean good = true; | |||||
for (Stop stop : stops) { | for (Stop stop : stops) { | ||||
if (shownStops.contains(stop.ID)){ | if (shownStops.contains(stop.ID)){ | ||||
Context not available. | |||||
continue; | continue; | ||||
shownStops.add(stop.ID); | shownStops.add(stop.ID); | ||||
if(!map.isShown()){ | |||||
if(good) | |||||
Log.d(DEBUG_TAG, "Need to show stop but map is not shown, probably detached already"); | |||||
good = false; | |||||
continue; | |||||
} else if(map.getRepository() == null){ | |||||
Log.e(DEBUG_TAG, "Map view repository is null"); | |||||
} | |||||
GeoPoint marker = new GeoPoint(stop.getLatitude(), stop.getLongitude()); | GeoPoint marker = new GeoPoint(stop.getLatitude(), stop.getLongitude()); | ||||
Marker stopMarker = makeMarker(marker, stop.getStopDefaultName(), stop.ID, false); | Marker stopMarker = makeMarker(marker, stop.getStopDefaultName(), stop.ID, false); | ||||
stopsFolderOverlay.add(stopMarker); | stopsFolderOverlay.add(stopMarker); | ||||
if (!map.getOverlays().contains(stopsFolderOverlay)) { | if (!map.getOverlays().contains(stopsFolderOverlay)) { | ||||
Log.w(DEBUG_TAG, "Map doesn't have folder overlay"); | Log.w(DEBUG_TAG, "Map doesn't have folder overlay"); | ||||
} | } | ||||
good=true; | |||||
} | } | ||||
//Log.d(DEBUG_TAG,"We have " +stopsFolderOverlay.getItems().size()+" stops in the folderOverlay"); | //Log.d(DEBUG_TAG,"We have " +stopsFolderOverlay.getItems().size()+" stops in the folderOverlay"); | ||||
//force redraw of markers | //force redraw of markers | ||||
Context not available. | |||||
public Marker makeMarker(GeoPoint geoPoint, String stopName, String ID, boolean isStartMarker) { | public Marker makeMarker(GeoPoint geoPoint, String stopName, String ID, boolean isStartMarker) { | ||||
// add a marker | // add a marker | ||||
Marker marker = new Marker(map); | final Marker marker = new Marker(map); | ||||
// 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); | ||||
Context not available. |
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