Search in sources :

Example 1 with MapFile

use of org.mapsforge.map.reader.MapFile in project satstat by mvglasow.

the class MapSectionFragment method createLayers.

/**
 * Creates layers and associated tile caches for the map view.
 *
 * @param createOverlays Whether to create overlays (circle and markers) or just tile layers
 */
private void createLayers(boolean createOverlays) {
    LayerManager layerManager = mapMap.getLayerManager();
    Layers layers = layerManager.getLayers();
    if (mainActivity.prefMapOffline && (ContextCompat.checkSelfPermission(mainActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) {
        /*
			 * If offline maps are enabled AND we have storage permission, use offline map tiles.
			 * Skip this step if we don't have permission, else we would pollute the cache with blank tiles.
			 */
        if (mapRendererTileCache == null)
            mapRendererTileCache = AndroidUtil.createExternalStorageTileCache(this.getContext(), Const.TILE_CACHE_INTERNAL_RENDER_THEME, Math.round(AndroidUtil.getMinimumCacheSize(this.getContext(), mapMap.getModel().displayModel.getTileSize(), mapMap.getModel().frameBufferModel.getOverdrawFactor(), 1f)), mapMap.getModel().displayModel.getTileSize(), true);
        /*
			 * If the offline map path changes, we need to purge the cache. This ensures we don't serve stale
			 * tiles generated from the old map set.
			 * 
			 * We accomplish this by comparing the current map path to the map path for which we last
			 * instantiated a cache. If they differ, we flush the cache and store the new map path.
			 */
        String cachedPath = mainActivity.mSharedPreferences.getString(Const.KEY_PREF_MAP_CACHED_PATH, "");
        if (!cachedPath.equals(mainActivity.prefMapPath)) {
            mapRendererTileCache.purge();
            SharedPreferences.Editor spEditor = mainActivity.mSharedPreferences.edit();
            spEditor.putString(Const.KEY_PREF_MAP_CACHED_PATH, mainActivity.prefMapPath);
            spEditor.commit();
        }
        MultiMapDataStore mapDataStore = new MultiMapDataStore(DataPolicy.DEDUPLICATE);
        File mapDir = new File(mainActivity.prefMapPath);
        Log.i(TAG, String.format("Looking for maps in: %s", mapDir.getName()));
        if (mapDir.exists() && mapDir.canRead() && mapDir.isDirectory())
            for (File file : mapDir.listFiles()) if (file.isFile())
                try {
                    MapFile mapFile = new MapFile(file);
                    mapDataStore.addMapDataStore(mapFile, false, false);
                    Log.i(TAG, String.format("Added map file: %s", file.getName()));
                } catch (MapFileException e) {
                    // not a map file, skip
                    Log.w(TAG, String.format("Could not add map file: %s", file.getName()));
                }
        mapRendererLayer = new TileRendererLayer(mapRendererTileCache, mapDataStore, mapMap.getModel().mapViewPosition, false, true, false, AndroidGraphicFactory.INSTANCE);
        mapRendererLayer.setXmlRenderTheme(InternalRenderTheme.OSMARENDER);
        // mapRendererLayer.setTextScale(1.5f); // FIXME
        layers.add(0, mapRendererLayer);
        mapAttribution.setText(R.string.osm_attribution);
    } else if (!mainActivity.prefMapOffline) {
        // use online map tiles
        if (mapDownloadTileCache == null)
            mapDownloadTileCache = AndroidUtil.createExternalStorageTileCache(this.getContext(), Const.TILE_CACHE_OSM, Math.round(AndroidUtil.getMinimumCacheSize(this.getContext(), mapMap.getModel().displayModel.getTileSize(), mapMap.getModel().frameBufferModel.getOverdrawFactor(), 1f)), mapMap.getModel().displayModel.getTileSize(), true);
        mapDownloadLayer = new TileDownloadLayer(mapDownloadTileCache, mapMap.getModel().mapViewPosition, onlineTileSource, AndroidGraphicFactory.INSTANCE);
        layers.add(0, mapDownloadLayer);
        /*
			 * Since tiles are now sourced from OSM (following Mapquest's decision to discontinue their free
			 * tile service), attribution is the same for online and offline. This may change if we switch
			 * to a different tile source (or allow multiple ones) - therefore, attribution should still
			 * depend on the map source.
			 */
        mapAttribution.setText(R.string.osm_attribution);
    } else
        mapAttribution.setText("");
    // parse list of location providers
    if (createOverlays)
        onLocationProvidersChanged(mainActivity.mSharedPreferences.getStringSet(Const.KEY_PREF_LOC_PROV, new HashSet<String>(Arrays.asList(new String[] { LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER }))));
    // mark cache as purged
    SharedPreferences.Editor spEditor = mainActivity.mSharedPreferences.edit();
    spEditor.putBoolean(Const.KEY_PREF_MAP_PURGE, false);
    spEditor.commit();
}
Also used : SharedPreferences(android.content.SharedPreferences) MapFileException(org.mapsforge.map.reader.header.MapFileException) TileRendererLayer(org.mapsforge.map.layer.renderer.TileRendererLayer) TileDownloadLayer(org.mapsforge.map.layer.download.TileDownloadLayer) MapFile(org.mapsforge.map.reader.MapFile) MultiMapDataStore(org.mapsforge.map.datastore.MultiMapDataStore) Layers(org.mapsforge.map.layer.Layers) MapFile(org.mapsforge.map.reader.MapFile) File(java.io.File) LayerManager(org.mapsforge.map.layer.LayerManager)

Aggregations

SharedPreferences (android.content.SharedPreferences)1 File (java.io.File)1 MultiMapDataStore (org.mapsforge.map.datastore.MultiMapDataStore)1 LayerManager (org.mapsforge.map.layer.LayerManager)1 Layers (org.mapsforge.map.layer.Layers)1 TileDownloadLayer (org.mapsforge.map.layer.download.TileDownloadLayer)1 TileRendererLayer (org.mapsforge.map.layer.renderer.TileRendererLayer)1 MapFile (org.mapsforge.map.reader.MapFile)1 MapFileException (org.mapsforge.map.reader.header.MapFileException)1