Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/ActivityMap.java
Show All 12 Lines | |||||
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.Marker; | import org.osmdroid.views.overlay.Marker; | ||||
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.MyLocationNewOverlay; | |||||
import java.util.List; | |||||
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 MAP_CURRENT_ZOOM_KEY = "map-current-zoom"; | |||||
private static final String MAP_CENTER_LAT_KEY = "map-center-lat"; | |||||
private static final String MAP_CENTER_LON_KEY = "map-center-lon"; | |||||
private static MapView map = null; | private static MapView map = null; | ||||
public Context ctx; | public Context ctx; | ||||
private MyLocationNewOverlay mLocationOverlay = null; | |||||
private FolderOverlay stopsFolderOverlay = null; | |||||
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB) | @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB) | ||||
@Override public void onCreate(Bundle savedInstanceState) { | @Override public void onCreate(Bundle savedInstanceState) { | ||||
super.onCreate(savedInstanceState); | super.onCreate(savedInstanceState); | ||||
//handle permissions first, before map is created. not depicted here | //handle permissions first, before map is created. not depicted here | ||||
//load/initialize the osmdroid configuration, this can be done | //load/initialize the osmdroid configuration, this can be done | ||||
Show All 9 Lines | @Override public void onCreate(Bundle savedInstanceState) { | ||||
setContentView(R.layout.activity_map); | setContentView(R.layout.activity_map); | ||||
map = (MapView) findViewById(R.id.map); | map = (MapView) findViewById(R.id.map); | ||||
map.setTileSource(TileSourceFactory.MAPNIK); | map.setTileSource(TileSourceFactory.MAPNIK); | ||||
// add ability to zoom with 2 fingers | // add ability to zoom with 2 fingers | ||||
map.setMultiTouchControls(true); | map.setMultiTouchControls(true); | ||||
//setup 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(); | ||||
GeoPoint marker = null; | GeoPoint marker = null; | ||||
String name = null; | String name = null; | ||||
String ID = null; | String ID = null; | ||||
if(b != null) { | if(b != null) { | ||||
double lat = b.getDouble("lat"); | double lat = b.getDouble("lat"); | ||||
double lon = b.getDouble("lon"); | double lon = b.getDouble("lon"); | ||||
marker = new GeoPoint(lat, lon); | marker = new GeoPoint(lat, lon); | ||||
name = b.getString("name"); | name = b.getString("name"); | ||||
ID = b.getString("ID"); | ID = b.getString("ID"); | ||||
} | } | ||||
startMap(marker, name, ID); | startMap(marker, name, ID); | ||||
// 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 19 Lines | public void startMap(GeoPoint marker, String name, String ID) { | ||||
} else { | } else { | ||||
startPoint = new GeoPoint(45.0708, 7.6858); | startPoint = new GeoPoint(45.0708, 7.6858); | ||||
mapController.setZoom(18.0); | mapController.setZoom(18.0); | ||||
} | } | ||||
// set the minimum zoom level | // set the minimum zoom level | ||||
map.setMinZoomLevel(15.5); | map.setMinZoomLevel(15.5); | ||||
mapController.setCenter(startPoint); | mapController.setCenter(startPoint); | ||||
// Location Overlay | |||||
// from OpenBikeSharing (THANK GOD) | |||||
GpsMyLocationProvider imlp = new GpsMyLocationProvider(this.getBaseContext()); | |||||
imlp.setLocationUpdateMinDistance(200); | |||||
imlp.setLocationUpdateMinTime(30000); | |||||
this.mLocationOverlay = new MyLocationNewOverlay(imlp,map); | |||||
this.mLocationOverlay.enableMyLocation(); | |||||
map.getOverlays().add(this.mLocationOverlay); | |||||
//add stops overlay | |||||
map.getOverlays().add(this.stopsFolderOverlay); | |||||
loadMarkers(); | loadMarkers(); | ||||
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); | makeMarker(startPoint, name , ID, true); | ||||
} | } | ||||
} | } | ||||
public void 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); | 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); | CustomInfoWindow popup = new CustomInfoWindow(map, ID, stopName); | ||||
marker.setInfoWindow(popup); | marker.setInfoWindow(popup); | ||||
Show All 24 Lines | public Marker makeMarker(GeoPoint geoPoint, String stopName, String ID, boolean isStartMarker) { | ||||
} | } | ||||
return true; | return true; | ||||
}); | }); | ||||
// set its position | // set its position | ||||
marker.setPosition(geoPoint); | marker.setPosition(geoPoint); | ||||
marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM); | marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM); | ||||
// display the marker | |||||
map.getOverlays().add(marker); | |||||
// add to it an icon | // add to it an icon | ||||
marker.setIcon(getResources().getDrawable(R.drawable.bus_marker)); | marker.setIcon(getResources().getDrawable(R.drawable.bus_marker)); | ||||
// 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(); | ||||
} | } | ||||
return marker; | |||||
} | } | ||||
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(); | |||||
List<Overlay> stopsOverlays = stopsFolderOverlay.getItems(); | |||||
if (stopsOverlays != null){ | |||||
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) { | ||||
GeoPoint marker = new GeoPoint(stop.getLatitude(), stop.getLongitude()); | GeoPoint marker = new GeoPoint(stop.getLatitude(), stop.getLongitude()); | ||||
makeMarker(marker, stop.getStopDefaultName(), stop.ID, false); | Marker stopMarker = makeMarker(marker, stop.getStopDefaultName(), stop.ID, false); | ||||
stopsFolderOverlay.add(stopMarker); | |||||
} | } | ||||
} | } | ||||
public void onResume(){ | public void onResume(){ | ||||
super.onResume(); | super.onResume(); | ||||
//this will refresh the osmdroid configuration on resuming. | //this will refresh the osmdroid configuration on resuming. | ||||
//if you make changes to the configuration, use | //if you make changes to the configuration, use | ||||
//SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); | //SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); | ||||
//Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this)); | //Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this)); | ||||
map.onResume(); //needed for compass, my location overlays, v6.0.0 and up | map.onResume(); //needed for compass, my location overlays, v6.0.0 and up | ||||
mLocationOverlay.enableMyLocation(); | |||||
} | } | ||||
public void onPause(){ | public void onPause(){ | ||||
super.onPause(); | super.onPause(); | ||||
//this will refresh the osmdroid configuration on resuming. | //this will refresh the osmdroid configuration on resuming. | ||||
//if you make changes to the configuration, use | //if you make changes to the configuration, use | ||||
//SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); | //SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); | ||||
//Configuration.getInstance().save(this, prefs); | //Configuration.getInstance().save(this, prefs); | ||||
map.onPause(); //needed for compass, my location overlays, v6.0.0 and up | map.onPause(); //needed for compass, my location overlays, v6.0.0 and up | ||||
mLocationOverlay.disableMyLocation(); | |||||
} | } | ||||
} | } | ||||
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