Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/ActivityMap.java
Show All 13 Lines | /* | ||||
You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License | ||||
along with this program. If not, see <http://www.gnu.org/licenses/>. | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
package it.reyboz.bustorino; | package it.reyboz.bustorino; | ||||
import android.Manifest; | |||||
import android.content.Context; | import android.content.Context; | ||||
import android.content.Intent; | import android.content.Intent; | ||||
import android.content.pm.PackageManager; | |||||
import android.location.Location; | import android.location.Location; | ||||
import android.location.LocationManager; | import android.location.LocationManager; | ||||
import android.os.Build; | import android.os.Build; | ||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.util.Log; | import android.util.Log; | ||||
import android.view.View; | import android.view.View; | ||||
import android.widget.ImageButton; | import android.widget.ImageButton; | ||||
import android.support.annotation.RequiresApi; | |||||
import android.support.v7.app.AppCompatActivity; | |||||
import android.support.v7.preference.PreferenceManager; | |||||
import android.widget.Toast; | import androidx.annotation.NonNull; | ||||
import androidx.annotation.RequiresApi; | |||||
import androidx.appcompat.app.AppCompatActivity; | |||||
import androidx.core.app.ActivityCompat; | |||||
import androidx.preference.PreferenceManager; | |||||
import it.reyboz.bustorino.middleware.GeneralActivity; | |||||
import it.reyboz.bustorino.middleware.NextGenDB; | import it.reyboz.bustorino.middleware.NextGenDB; | ||||
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.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.*; | 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; | |||||
public class ActivityMap extends AppCompatActivity { | public class ActivityMap extends GeneralActivity { | ||||
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"; | ||||
Show All 10 Lines | public class ActivityMap extends GeneralActivity { | ||||
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; | ||||
@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 | ||||
ctx = getApplicationContext(); | ctx = getApplicationContext(); | ||||
Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx)); | Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx)); | ||||
//setting this before the layout is inflated is a good idea | //setting this before the layout is inflated is a good idea | ||||
//it 'should' ensure that the map has a writable location for the map cache, even without permissions | //it 'should' ensure that the map has a writable location for the map cache, even without permissions | ||||
//if no tiles are displayed, you can try overriding the cache path using Configuration.getInstance().setCachePath | //if no tiles are displayed, you can try overriding the cache path using Configuration.getInstance().setCachePath | ||||
//see also StorageUtils | //see also StorageUtils | ||||
//note, the load method also sets the HTTP User Agent to your application's package name, abusing osm's tile servers will get you banned based on this string | //note, the load method also sets the HTTP User Agent to your application's package name, abusing osm's tile servers will get you banned based on this string | ||||
Show All 32 Lines | public void onCreate(Bundle savedInstanceState) { | ||||
public boolean onZoom(ZoomEvent event) { | public boolean onZoom(ZoomEvent event) { | ||||
loadMarkers(); | loadMarkers(); | ||||
return true; | return true; | ||||
} | } | ||||
})); | })); | ||||
btCenterMap.setOnClickListener(new View.OnClickListener() { | btCenterMap.setOnClickListener(new View.OnClickListener() { | ||||
@Override | @Override | ||||
public void onClick(View v) { | public void onClick(View v) { | ||||
Log.i(TAG, "centerMap clicked "); | Log.i(TAG, "centerMap clicked "); | ||||
final GeoPoint myPosition = mLocationOverlay.getMyLocation(); | final GeoPoint myPosition = mLocationOverlay.getMyLocation(); | ||||
map.getController().animateTo(myPosition); | map.getController().animateTo(myPosition); | ||||
} | } | ||||
}); | }); | ||||
Show All 10 Lines | public void onCreate(Bundle savedInstanceState) { | ||||
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<>(); | 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; | ||||
boolean havePositionPermission = true; | |||||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | |||||
askForPermissionIfNeeded(Manifest.permission.ACCESS_FINE_LOCATION, PERMISSION_REQUEST_POSITION); | |||||
havePositionPermission = false; | |||||
} | |||||
if (marker != null) { | if (marker != null) { | ||||
startPoint = marker; | startPoint = marker; | ||||
mapController.setZoom(POSITION_FOUND_ZOOM); | mapController.setZoom(POSITION_FOUND_ZOOM); | ||||
} | } else if (savedInstanceState != null || !havePositionPermission) { | ||||
else if (savedInstanceState != null) { | |||||
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))); | ||||
} else { | } else { | ||||
boolean found = false; | boolean found = false; | ||||
LocationManager locationManager = | LocationManager locationManager = | ||||
(LocationManager) this.getSystemService(Context.LOCATION_SERVICE); | (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); | ||||
if(locationManager!=null) { | if (locationManager != null) { | ||||
Location userLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); | Location userLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); | ||||
if (userLocation != null) { | if (userLocation != null) { | ||||
mapController.setZoom(POSITION_FOUND_ZOOM); | mapController.setZoom(POSITION_FOUND_ZOOM); | ||||
startPoint = new GeoPoint(userLocation); | startPoint = new GeoPoint(userLocation); | ||||
found = true; | found = true; | ||||
} | } | ||||
} | } | ||||
if(!found){ | if(!found){ | ||||
▲ Show 20 Lines • Show All 177 Lines • ▼ Show 20 Lines | public class ActivityMap extends GeneralActivity { | ||||
@Override | @Override | ||||
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()); | ||||
} | } | ||||
/** | |||||
* PERMISSION STUFF | |||||
**/ | |||||
@Override | |||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |||||
switch (requestCode) { | |||||
case PERMISSION_REQUEST_POSITION: | |||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |||||
setOption(LOCATION_PERMISSION_GIVEN, true); | |||||
//if we sent a request for a new NearbyStopsFragment | |||||
} else { | |||||
//permission denied | |||||
setOption(LOCATION_PERMISSION_GIVEN, false); | |||||
} | |||||
break; | |||||
//add other cases for permissions | |||||
} | |||||
} | |||||
} | } | ||||
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