Search in sources :

Example 1 with LayerManager

use of org.mapsforge.map.layer.LayerManager in project satstat by mvglasow.

the class MapSectionFragment method applyLocationProviderStyle.

/**
 * Applies a style to the map overlays associated with a given location provider.
 *
 * This method changes the style (effectively, the color) of the circle and
 * marker overlays. Its main purpose is to switch the color of the overlays
 * between gray and the provider color.
 *
 * @param context The context of the caller
 * @param provider The name of the location provider, as returned by
 * {@link LocationProvider.getName()}.
 * @param styleName The name of the style to apply. If it is null, the
 * default style for the provider as returned by
 * assignLocationProviderStyle() is applied.
 */
protected void applyLocationProviderStyle(Context context, String provider, String styleName) {
    String sn = (styleName != null) ? styleName : assignLocationProviderStyle(provider);
    Boolean isStyleChanged = !sn.equals(providerAppliedStyles.get(provider));
    Boolean needsRedraw = false;
    Resources res = context.getResources();
    TypedArray style = res.obtainTypedArray(res.getIdentifier(sn, "array", context.getPackageName()));
    // Circle layer
    Circle circle = mapCircles.get(provider);
    if (circle != null) {
        circle.getPaintFill().setColor(style.getColor(Const.STYLE_FILL, R.color.circle_gray_fill));
        circle.getPaintStroke().setColor(style.getColor(Const.STYLE_STROKE, R.color.circle_gray_stroke));
        needsRedraw = isStyleChanged && circle.isVisible();
    }
    // Marker layer
    Marker marker = mapMarkers.get(provider);
    if (marker != null) {
        Drawable drawable = style.getDrawable(Const.STYLE_MARKER);
        Bitmap bitmap = AndroidGraphicFactory.convertToBitmap(drawable);
        marker.setBitmap(bitmap);
        needsRedraw = needsRedraw || (isStyleChanged && marker.isVisible());
    }
    if (needsRedraw) {
        LayerManager manager = mapMap.getLayerManager();
        if (manager != null)
            manager.redrawLayers();
    }
    providerAppliedStyles.put(provider, sn);
    style.recycle();
}
Also used : Circle(org.mapsforge.map.layer.overlay.Circle) Bitmap(org.mapsforge.core.graphics.Bitmap) TypedArray(android.content.res.TypedArray) Drawable(android.graphics.drawable.Drawable) Resources(android.content.res.Resources) Marker(org.mapsforge.map.layer.overlay.Marker) LayerManager(org.mapsforge.map.layer.LayerManager)

Example 2 with LayerManager

use of org.mapsforge.map.layer.LayerManager 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

LayerManager (org.mapsforge.map.layer.LayerManager)2 SharedPreferences (android.content.SharedPreferences)1 Resources (android.content.res.Resources)1 TypedArray (android.content.res.TypedArray)1 Drawable (android.graphics.drawable.Drawable)1 File (java.io.File)1 Bitmap (org.mapsforge.core.graphics.Bitmap)1 MultiMapDataStore (org.mapsforge.map.datastore.MultiMapDataStore)1 Layers (org.mapsforge.map.layer.Layers)1 TileDownloadLayer (org.mapsforge.map.layer.download.TileDownloadLayer)1 Circle (org.mapsforge.map.layer.overlay.Circle)1 Marker (org.mapsforge.map.layer.overlay.Marker)1 TileRendererLayer (org.mapsforge.map.layer.renderer.TileRendererLayer)1 MapFile (org.mapsforge.map.reader.MapFile)1 MapFileException (org.mapsforge.map.reader.header.MapFileException)1