Example #1
0
 /**
  * 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);
     }
   }
 }
Example #2
0
  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);
  }
Example #3
0
  /**
   * 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();
    }
  }