Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/ActivityMap.java
- This file was added.
package it.reyboz.bustorino; | |||||
import android.content.Context; | |||||
import android.os.Bundle; | |||||
import android.support.v7.app.AppCompatActivity; | |||||
import android.support.v7.preference.PreferenceManager; | |||||
import org.osmdroid.api.IMapController; | |||||
import org.osmdroid.config.Configuration; | |||||
import org.osmdroid.tileprovider.tilesource.TileSourceFactory; | |||||
import org.osmdroid.util.GeoPoint; | |||||
import org.osmdroid.views.MapView; | |||||
import org.osmdroid.views.overlay.Marker; | |||||
public class ActivityMap extends AppCompatActivity { | |||||
private static MapView map = null; | |||||
@Override public void onCreate(Bundle savedInstanceState) { | |||||
super.onCreate(savedInstanceState); | |||||
//handle permissions first, before map is created. not depicted here | |||||
//load/initialize the osmdroid configuration, this can be done | |||||
Context ctx = getApplicationContext(); | |||||
Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx)); | |||||
//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 | |||||
//if no tiles are displayed, you can try overriding the cache path using Configuration.getInstance().setCachePath | |||||
//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 | |||||
//inflate and create the map | |||||
setContentView(R.layout.activity_map); | |||||
map = (MapView) findViewById(R.id.map); | |||||
map.setTileSource(TileSourceFactory.MAPNIK); | |||||
// add ability to zoom with 2 fingers | |||||
map.setMultiTouchControls(true); | |||||
Bundle b = getIntent().getExtras(); | |||||
double lat = -1; | |||||
double lon = -1; | |||||
GeoPoint marker = null; | |||||
if(b != null) { | |||||
lat = b.getDouble("lat"); | |||||
lon = b.getDouble("lon"); | |||||
marker = new GeoPoint(lat, lon); | |||||
} | |||||
startMap(marker); | |||||
} | |||||
public static void startMap(GeoPoint marker) { | |||||
// move the map on the marker position or on a default view point: Turin, Piazza Castello | |||||
IMapController mapController = map.getController(); | |||||
mapController.setZoom(18.0); | |||||
GeoPoint startPoint; | |||||
if (marker != null) { | |||||
startPoint = marker; | |||||
} else { | |||||
startPoint = new GeoPoint(45.0708, 7.6858); | |||||
} | |||||
mapController.setCenter(startPoint); | |||||
if (marker != null) { | |||||
// add a marker | |||||
Marker startMarker = new Marker(map); | |||||
startMarker.setPosition(marker); | |||||
startMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM); | |||||
map.getOverlays().add(startMarker); | |||||
} | |||||
} | |||||
public void onResume(){ | |||||
super.onResume(); | |||||
//this will refresh the osmdroid configuration on resuming. | |||||
//if you make changes to the configuration, use | |||||
//SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); | |||||
//Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this)); | |||||
map.onResume(); //needed for compass, my location overlays, v6.0.0 and up | |||||
} | |||||
public void onPause(){ | |||||
super.onPause(); | |||||
//this will refresh the osmdroid configuration on resuming. | |||||
//if you make changes to the configuration, use | |||||
//SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); | |||||
//Configuration.getInstance().save(this, prefs); | |||||
map.onPause(); //needed for compass, my location overlays, v6.0.0 and up | |||||
} | |||||
} | |||||
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