Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/ActivityMap.java
Show First 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | |||||
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.Overlay; | import org.osmdroid.views.overlay.Overlay; | ||||
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 org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay; | import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay; | ||||
import java.util.List; | import java.util.*; | ||||
import it.reyboz.bustorino.backend.Stop; | import it.reyboz.bustorino.backend.Stop; | ||||
import it.reyboz.bustorino.map.CustomInfoWindow; | import it.reyboz.bustorino.map.CustomInfoWindow; | ||||
import it.reyboz.bustorino.middleware.StopsDB; | import it.reyboz.bustorino.middleware.StopsDB; | ||||
public class ActivityMap extends AppCompatActivity { | public class ActivityMap extends AppCompatActivity { | ||||
private static final String TAG = "Busto-MapActivity"; | private static final String TAG = "Busto-MapActivity"; | ||||
private static final String MAP_CURRENT_ZOOM_KEY = "map-current-zoom"; | private static final String MAP_CURRENT_ZOOM_KEY = "map-current-zoom"; | ||||
private static final String MAP_CENTER_LAT_KEY = "map-center-lat"; | private static final String MAP_CENTER_LAT_KEY = "map-center-lat"; | ||||
private static final String MAP_CENTER_LON_KEY = "map-center-lon"; | private static final String MAP_CENTER_LON_KEY = "map-center-lon"; | ||||
public static final String BUNDLE_LATIT = "lat"; | public static final String BUNDLE_LATIT = "lat"; | ||||
public static final String BUNDLE_LONGIT = "lon"; | public static final String BUNDLE_LONGIT = "lon"; | ||||
public static final String BUNDLE_NAME = "name"; | public static final String BUNDLE_NAME = "name"; | ||||
public static final String BUNDLE_ID = "ID"; | public static final String BUNDLE_ID = "ID"; | ||||
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.6; | private static final double POSITION_FOUND_ZOOM = 18.3; | ||||
private HashSet<String> shownStops = null; | |||||
private MapView map = null; | private MapView map = null; | ||||
public Context ctx; | public Context ctx; | ||||
private MyLocationNewOverlay mLocationOverlay = null; | private MyLocationNewOverlay mLocationOverlay = null; | ||||
private FolderOverlay stopsFolderOverlay = null; | private FolderOverlay stopsFolderOverlay = null; | ||||
protected ImageButton btCenterMap; | protected ImageButton btCenterMap; | ||||
protected ImageButton btFollowMe; | protected ImageButton btFollowMe; | ||||
Show All 30 Lines | @Override public void onCreate(Bundle savedInstanceState) { | ||||
//setup FolderOverlay | //setup FolderOverlay | ||||
stopsFolderOverlay = new FolderOverlay(); | stopsFolderOverlay = new FolderOverlay(); | ||||
// take the parameters if it's called from other Activities | // take the parameters if it's called from other Activities | ||||
Bundle b = getIntent().getExtras(); | Bundle b = getIntent().getExtras(); | ||||
startMap(b, savedInstanceState); | startMap(b, savedInstanceState); | ||||
// on drag and zoom reload the markers | // on drag and zoom reload the markers | ||||
map.addMapListener(new DelayedMapListener(new MapListener() { | map.addMapListener(new DelayedMapListener(new MapListener() { | ||||
@Override | @Override | ||||
public boolean onScroll(ScrollEvent paramScrollEvent) { | public boolean onScroll(ScrollEvent paramScrollEvent) { | ||||
loadMarkers(); | loadMarkers(); | ||||
return true; | return true; | ||||
} | } | ||||
Show All 26 Lines | @Override public void onCreate(Bundle savedInstanceState) { | ||||
mLocationOverlay.enableFollowLocation(); | mLocationOverlay.enableFollowLocation(); | ||||
btFollowMe.setImageResource(R.drawable.ic_follow_me_on); | btFollowMe.setImageResource(R.drawable.ic_follow_me_on); | ||||
} else { | } else { | ||||
mLocationOverlay.disableFollowLocation(); | mLocationOverlay.disableFollowLocation(); | ||||
btFollowMe.setImageResource(R.drawable.ic_follow_me); | btFollowMe.setImageResource(R.drawable.ic_follow_me); | ||||
} | } | ||||
} | } | ||||
}); | }); | ||||
} | } | ||||
public void startMap(Bundle incoming, Bundle savedInstanceState) { | public void startMap(Bundle incoming, Bundle savedInstanceState) { | ||||
//parse incoming bundle | //parse incoming bundle | ||||
GeoPoint marker = null; | GeoPoint marker = null; | ||||
String name = null; | String name = null; | ||||
String ID = null; | String ID = null; | ||||
if(incoming != null) { | if(incoming != null) { | ||||
double lat = incoming.getDouble(BUNDLE_LATIT); | double lat = incoming.getDouble(BUNDLE_LATIT); | ||||
double lon = incoming.getDouble(BUNDLE_LONGIT); | double lon = incoming.getDouble(BUNDLE_LONGIT); | ||||
marker = new GeoPoint(lat, lon); | marker = new GeoPoint(lat, lon); | ||||
name = incoming.getString(BUNDLE_NAME); | name = incoming.getString(BUNDLE_NAME); | ||||
ID = incoming.getString(BUNDLE_ID); | ID = incoming.getString(BUNDLE_ID); | ||||
} | } | ||||
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; | ||||
if (marker != null) { | if (marker != null) { | ||||
startPoint = marker; | startPoint = marker; | ||||
mapController.setZoom(POSITION_FOUND_ZOOM); | mapController.setZoom(POSITION_FOUND_ZOOM); | ||||
Show All 25 Lines | public void startMap(Bundle incoming, Bundle savedInstanceState) { | ||||
//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); | ||||
} | } | ||||
// Location Overlay | // Location Overlay | ||||
// from OpenBikeSharing (THANK GOD) | // from OpenBikeSharing (THANK GOD) | ||||
GpsMyLocationProvider imlp = new GpsMyLocationProvider(this.getBaseContext()); | GpsMyLocationProvider imlp = new GpsMyLocationProvider(this.getBaseContext()); | ||||
imlp.setLocationUpdateMinDistance(10); | imlp.setLocationUpdateMinDistance(5); | ||||
imlp.setLocationUpdateMinTime(2000); | imlp.setLocationUpdateMinTime(2000); | ||||
this.mLocationOverlay = new MyLocationNewOverlay(imlp,map); | this.mLocationOverlay = new MyLocationNewOverlay(imlp,map); | ||||
mLocationOverlay.enableMyLocation(); | mLocationOverlay.enableMyLocation(); | ||||
mLocationOverlay.enableFollowLocation(); | mLocationOverlay.enableFollowLocation(); | ||||
btFollowMe.setImageResource(R.drawable.ic_follow_me_on); | |||||
mLocationOverlay.setOptionsMenuEnabled(true); | mLocationOverlay.setOptionsMenuEnabled(true); | ||||
/* | /* | ||||
mLocationOverlay.runOnFirstFix(() -> { | mLocationOverlay.runOnFirstFix(() -> { | ||||
mapController.setCenter(mLocationOverlay.getMyLocation()); | mapController.setCenter(mLocationOverlay.getMyLocation()); | ||||
mapController.animateTo(mLocationOverlay.getMyLocation()); | mapController.animateTo(mLocationOverlay.getMyLocation()); | ||||
}); | }); | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | public class ActivityMap extends AppCompatActivity { | ||||
} | } | ||||
public void loadMarkers() { | public void loadMarkers() { | ||||
// get rid of the previous markers | // get rid of the previous markers | ||||
//map.getOverlays().clear(); | //map.getOverlays().clear(); | ||||
//stopsFolderOverlay = new FolderOverlay(); | //stopsFolderOverlay = new FolderOverlay(); | ||||
List<Overlay> stopsOverlays = stopsFolderOverlay.getItems(); | List<Overlay> stopsOverlays = stopsFolderOverlay.getItems(); | ||||
if (stopsOverlays != null){ | /*if (stopsOverlays != null){ | ||||
stopsOverlays.clear(); | stopsOverlays.clear(); | ||||
} | }*/ | ||||
// get the top, bottom, left and right screen's coordinate | // get the top, bottom, left and right screen's coordinate | ||||
BoundingBox bb = map.getBoundingBox(); | BoundingBox bb = map.getBoundingBox(); | ||||
double latFrom = bb.getLatSouth(); | double latFrom = bb.getLatSouth(); | ||||
double latTo = bb.getLatNorth(); | double latTo = bb.getLatNorth(); | ||||
double lngFrom = bb.getLonWest(); | double lngFrom = bb.getLonWest(); | ||||
double lngTo = bb.getLonEast(); | double lngTo = bb.getLonEast(); | ||||
// get the stops located in those coordinates | // get the stops located in those coordinates | ||||
StopsDB stopsDB = new StopsDB(ctx); | StopsDB stopsDB = new StopsDB(ctx); | ||||
stopsDB.openIfNeeded(); | stopsDB.openIfNeeded(); | ||||
Stop[] stops = stopsDB.queryAllInsideMapView(latFrom, latTo, lngFrom, lngTo); | Stop[] stops = stopsDB.queryAllInsideMapView(latFrom, latTo, lngFrom, lngTo); | ||||
stopsDB.closeIfNeeded(); | stopsDB.closeIfNeeded(); | ||||
// add new markers of those stops | // add new markers of those stops | ||||
for (Stop stop : stops) { | for (Stop stop : stops) { | ||||
if (shownStops.contains(stop.ID)){ | |||||
continue; | |||||
} | |||||
try{ | |||||
stop.getLatitude(); | |||||
stop.getLongitude(); | |||||
} catch (NullPointerException e) { | |||||
Log.e(TAG,"Stop "+stop.ID+ " gives null coordinates"); | |||||
e.printStackTrace(); | |||||
continue; | |||||
} | |||||
shownStops.add(stop.ID); | |||||
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); | ||||
} | } | ||||
} | } | ||||
protected boolean detachMapFromPosition(){ | protected boolean detachMapFromPosition(){ | ||||
if (mLocationOverlay.isFollowLocationEnabled()) { | if (mLocationOverlay.isFollowLocationEnabled()) { | ||||
mLocationOverlay.disableFollowLocation(); | mLocationOverlay.disableFollowLocation(); | ||||
btFollowMe.setImageResource(R.drawable.ic_follow_me); | btFollowMe.setImageResource(R.drawable.ic_follow_me); | ||||
Show All 24 Lines | public class ActivityMap extends AppCompatActivity { | ||||
protected void onSaveInstanceState(Bundle outState) { | protected void onSaveInstanceState(Bundle outState) { | ||||
super.onSaveInstanceState(outState); | super.onSaveInstanceState(outState); | ||||
outState.putDouble(MAP_CURRENT_ZOOM_KEY, map.getZoomLevelDouble()); | outState.putDouble(MAP_CURRENT_ZOOM_KEY, map.getZoomLevelDouble()); | ||||
outState.putDouble(MAP_CENTER_LAT_KEY, map.getMapCenter().getLatitude()); | outState.putDouble(MAP_CENTER_LAT_KEY, map.getMapCenter().getLatitude()); | ||||
outState.putDouble(MAP_CENTER_LON_KEY, map.getMapCenter().getLongitude()); | outState.putDouble(MAP_CENTER_LON_KEY, map.getMapCenter().getLongitude()); | ||||
} | } | ||||
} | } | ||||
No newline at end of file | No newline at end of file |
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