/** * Creates/Restore the layer switcher or restore the old one and add all other menu * * @param savedInstanceState * @param layerManager */ private void setupLeftMenu(Bundle savedInstanceState, MultiSourceOverlayManager layerManager) { // work on fragment management FragmentManager fManager = getSupportFragmentManager(); LayerSwitcherFragment osf; if (savedInstanceState != null) { osf = (LayerSwitcherFragment) fManager.findFragmentById(R.id.left_drawer_container); if (osf == null) { Log.e("MAPSACTIVITY", "unable to restore layer switcher"); } layerManager.setLayerChangeListener(osf); layerManager.restoreInstanceState(savedInstanceState); } else { layerManager.defaultInit(); // @SuppressWarnings("unchecked") // ArrayList<Layer> layers = (ArrayList<Layer>) LocalPersistence.readObjectFromFile(this, // LocalPersistence.CURRENT_MAP); // if(layers != null){ // layerManager.setLayers(layers); // }else{ if (getIntent() != null && getIntent().getExtras() != null && getIntent().getExtras().containsKey(MSM_MAP)) { layerManager.loadMap((MSMMap) getIntent().getExtras().getSerializable(MSM_MAP)); } else { boolean dontLoadMBTileLayer = MapFilesProvider.getBackgroundSourceType() == BackgroundSourceType.MBTILES; MSMMap map = SpatialDbUtils.mapFromDb(dontLoadMBTileLayer); StorageUtils.setupSources(this); // This adds layers also if its called loadMap but it will not order layers // layerManager.loadMap(map); // so use this instead addLayersOrdered(map.layers); } // } // setup left drawer fragments if (mDrawerMode != DrawerMode.NONE) { osf = new LayerSwitcherFragment(); layerManager.setLayerChangeListener(osf); FragmentTransaction fragmentTransaction = fManager.beginTransaction(); fragmentTransaction.add(R.id.left_drawer_container, osf); GenericMenuFragment other = new GenericMenuFragment(); // fragmentTransaction.add(R.id.right_drawer, other); if (mDrawerMode == DrawerMode.BOTH) { SourcesFragment sf = new SourcesFragment(); fragmentTransaction.add(R.id.right_drawer, sf); } fragmentTransaction.commit(); } } }
/** * Add layers to the map * * @param layersToAdd */ private void addLayers(ArrayList<Layer> layersToAdd) { ArrayList<Layer> layers = new ArrayList<Layer>(layerManager.getLayers()); layers.addAll(layersToAdd); layerManager.setLayers(layers); // close right drawer if (mLayerMenu != null) { if (mDrawerLayout.isDrawerOpen(mLayerMenu)) { mDrawerLayout.closeDrawer(mLayerMenu); } } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.form_mapview); mapView = (AdvancedMapView) findViewById(R.id.advancedMapView); ImageButton buttonLocation = (ImageButton) findViewById(R.id.ButtonLocation); // mapView = new AdvancedMapView(this); initMap(savedInstanceState); MultiSourceOverlayManager o = new MultiSourceOverlayManager(mapView); o.setMarkerOverlay(new MarkerOverlay()); o.setMarkerVisible(); mapView.setOverlayManger(o); if (getIntent() != null && getIntent().getExtras() != null && getIntent().getExtras().containsKey(MapsActivity.MSM_MAP)) { o.loadMap((MSMMap) getIntent().getExtras().getSerializable(MapsActivity.MSM_MAP)); } mapView.setLayoutParams( new LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)); mapView.setClickable(true); mapView.setBuiltInZoomControls(true); // add coordinates control mapView.addControl(new CoordinateControl(mapView, true)); // add "location" control connected to the button LocationControl lc = new LocationControl(mapView); lc.setActivationButton(buttonLocation); mapView.addControl(lc); centerMapFileAndAddMarkers(); }
public void addLayersOrdered(final ArrayList<Layer> layers) { ArrayList<Layer> originalLayers = layers; ArrayList<Layer> orderedLayers = new ArrayList<Layer>(); // check if there is a MBTiles layer which needs to be ordered boolean layersContainMBTilesLayer = false; for (Layer l : originalLayers) { if (l instanceof MbTilesLayer) { layersContainMBTilesLayer = true; break; } } // if there is, add this flag to wait until it has been added boolean mbTilesAdded = !layersContainMBTilesLayer; while (!originalLayers.isEmpty()) { final Layer layer = originalLayers.get(originalLayers.size() - 1); // get last if (layer instanceof MbTilesLayer) { final int currentValue = PreferenceManager.getDefaultSharedPreferences(getBaseContext()) .getInt(MBTilesLayerOpacitySettingActivity.MBTILES_OPACITY_ID, 192); layer.setOpacity(currentValue); orderedLayers.add(layer); mbTilesAdded = true; originalLayers.remove(layer); Log.d( MapsActivity.class.getSimpleName(), "mbtiles layer added , size " + orderedLayers.size()); } else if (mbTilesAdded) { orderedLayers.add(layer); originalLayers.remove(layer); Log.d(MapsActivity.class.getSimpleName(), "other added , size " + orderedLayers.size()); } } layerManager.setLayers(orderedLayers); }
/* (non-Javadoc) * @see android.app.Activity#onActivityResult(int, int, android.content.Intent) */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.d(MapsActivity.class.getSimpleName(), "MapsActivity onActivityResult"); if (requestCode == LayerSwitcherFragment.OPACITY_SETTIN_REQUEST_ID) { final int newValue = PreferenceManager.getDefaultSharedPreferences(getBaseContext()) .getInt(MBTilesLayerOpacitySettingActivity.MBTILES_OPACITY_ID, 192); ArrayList<Layer> layers = layerManager.getLayers(); for (Layer l : layers) { if (l instanceof MbTilesLayer) { l.setOpacity(newValue); layerManager.redrawLayer(l); } } // its not necessary to handle the other stuff return; } if (requestCode == GetFeatureInfoLayerListActivity.BBOX_REQUEST && resultCode == RESULT_OK) { // the response can contain a feature to use to replace the current marker // on the map manageMarkerSubstitutionAction(data); } // controls can be refreshed getting the result of an intent, in this case // each control knows which intent he sent with their requestCode/resultCode for (MapControl control : mapView.getControls()) { control.refreshControl(requestCode, resultCode, data); } // reload stores in the panel (we do it everyTime, maybe there is a better way SourcesFragment sf = (SourcesFragment) getSupportFragmentManager().findFragmentById(R.id.right_drawer); if (sf != null) { sf.reloadStores(); } // manager mapstore configuration load // TODO: with the new interface this will load a map instead of the mapstoreconfig if (data == null) return; Bundle b = data.getExtras(); if (requestCode == DATAPROPERTIES_REQUEST_CODE) { mapView.getOverlayController().redrawOverlays(); // close right drawer if (mLayerMenu != null) { if (mDrawerLayout.isDrawerOpen(mLayerMenu)) { mDrawerLayout.closeDrawer(mLayerMenu); } } } Resource resource = (Resource) data.getSerializableExtra(GeoStoreResourceDetailActivity.PARAMS.RESOURCE); if (resource != null) { String geoStoreUrl = data.getStringExtra(GeoStoreResourcesActivity.PARAMS.GEOSTORE_URL); loadGeoStoreResource(resource, geoStoreUrl); } if (b.containsKey(MAPSTORE_CONFIG)) { overlayManager.loadMapStoreConfig((MapStoreConfiguration) b.getSerializable(MAPSTORE_CONFIG)); } if (b.containsKey(MSM_MAP)) { layerManager.loadMap((MSMMap) b.getSerializable(MSM_MAP)); } ArrayList<Layer> layersToAdd = (ArrayList<Layer>) b.getSerializable(LAYERS_TO_ADD); if (layersToAdd != null) { addLayers(layersToAdd); } }
/** * checks if the preferences of the background renderer changed if so, the mapview is informed and * is cleared and redrawed */ public void checkIfMapViewNeedsBackgroundUpdate() { final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); final boolean thingsChanged = prefs.getBoolean(MapView.MAPSFORGE_BACKGROUND_FILEPATH_CHANGED, false); if (!thingsChanged) return; final BackgroundSourceType currentMapRendererType = this.mapView.getMapRendererType(); String filePath = prefs.getString(MapView.MAPSFORGE_BACKGROUND_FILEPATH, null); final String defaultType = getApplicationContext().getPackageName().equals("it.geosolutions.geocollect.android.app") ? "1" : "0"; BackgroundSourceType type = BackgroundSourceType.values()[ Integer.parseInt( prefs.getString(MapView.MAPSFORGE_BACKGROUND_RENDERER_TYPE, defaultType))]; final Editor ed = prefs.edit(); ed.putBoolean(MapView.MAPSFORGE_BACKGROUND_FILEPATH_CHANGED, false); ed.commit(); File mapFile = new File(filePath); if (mapFile == null || !mapFile.exists()) { mapFile = MapFilesProvider.getBackgroundMapFile(); filePath = mapFile.getPath(); type = BackgroundSourceType.MAPSFORGE; } // 1. renderer changed if (type != currentMapRendererType) { MapRenderer mapRenderer = null; switch (type) { case MAPSFORGE: if (filePath == null) { throw new IllegalArgumentException( "no filepath selected to change to mapsforge renderer"); } mapView.setMapFile(new File(filePath)); mapRenderer = new DatabaseRenderer(mapView.getMapDatabase()); // TODO it was MBTILES with no or dimmed mbtiles layer, add MBTiles layer ? MSMMap map = SpatialDbUtils.mapFromDb(false); Log.d( MapsActivity.class.getSimpleName(), "to mapsforge maps includes " + map.layers.size() + " layers"); addLayersOrdered(map.layers); break; case MBTILES: mapRenderer = new MbTilesDatabaseRenderer(getBaseContext(), filePath); MSMMap map2 = SpatialDbUtils.mapFromDb(true); layerManager.setLayers(map2.layers); break; case GEOCOLLECT: // TODO break; default: break; } if (mDrawerToggle != null) { mDrawerToggle.syncState(); } mapView.setRenderer(mapRenderer, true); mapView.clearAndRedrawMapView(); MapFilesProvider.setBackgroundSourceType(type); } else if (filePath != null && !filePath.equals(mapView.getMapRenderer().getFileName())) { // 2.renderer is the same but file changed switch (type) { case MAPSFORGE: mapView.setMapFile(new File(filePath)); break; case MBTILES: mapView.setRenderer(new MbTilesDatabaseRenderer(getBaseContext(), filePath), true); break; case GEOCOLLECT: // TODO break; default: break; } mapView.clearAndRedrawMapView(); } }