Search in sources :

Example 1 with TileRendererLayer

use of org.mapsforge.map.layer.renderer.TileRendererLayer in project RSAndroidApp by RailwayStations.

the class MapsActivity method createLayers.

protected void createLayers() {
    final String map = baseApplication.getMap();
    final Uri mapUri = baseApplication.toUri(map);
    final MapDataStore mapFile = getMapFile(mapUri);
    if (mapFile != null) {
        final TileRendererLayer rendererLayer = new TileRendererLayer(this.tileCaches.get(0), mapFile, this.binding.map.mapView.getModel().mapViewPosition, false, true, false, AndroidGraphicFactory.INSTANCE) {

            @Override
            public boolean onLongPress(final LatLong tapLatLong, final Point thisXY, final Point tapXY) {
                MapsActivity.this.onLongPress(tapLatLong);
                return true;
            }
        };
        rendererLayer.setXmlRenderTheme(getRenderTheme());
        this.layer = rendererLayer;
        binding.map.mapView.getLayerManager().getLayers().add(this.layer);
    } else {
        AbstractTileSource tileSource = onlineTileSources.get(map);
        if (tileSource == null) {
            tileSource = OpenStreetMapMapnik.INSTANCE;
        }
        tileSource.setUserAgent(USER_AGENT);
        this.layer = new TileDownloadLayer(this.tileCaches.get(0), this.binding.map.mapView.getModel().mapViewPosition, tileSource, AndroidGraphicFactory.INSTANCE) {

            @Override
            public boolean onLongPress(final LatLong tapLatLong, final Point thisXY, final Point tapXY) {
                MapsActivity.this.onLongPress(tapLatLong);
                return true;
            }
        };
        binding.map.mapView.getLayerManager().getLayers().add(this.layer);
        binding.map.mapView.setZoomLevelMin(tileSource.getZoomLevelMin());
        binding.map.mapView.setZoomLevelMax(tileSource.getZoomLevelMax());
    }
}
Also used : MapDataStore(org.mapsforge.map.datastore.MapDataStore) TileRendererLayer(org.mapsforge.map.layer.renderer.TileRendererLayer) TileDownloadLayer(org.mapsforge.map.layer.download.TileDownloadLayer) Point(org.mapsforge.core.model.Point) Uri(android.net.Uri) LatLong(org.mapsforge.core.model.LatLong) AbstractTileSource(org.mapsforge.map.layer.download.tilesource.AbstractTileSource)

Example 2 with TileRendererLayer

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

the class MapSectionFragment method onLocationProvidersChanged.

/**
 * Updates internal data structures when the user's selection of location providers has changed.
 * @param providers The new set of location providers
 */
public void onLocationProvidersChanged(Set<String> providers) {
    Context context = this.getContext();
    List<String> allProviders = mainActivity.locationManager.getAllProviders();
    ArrayList<String> removedProviders = new ArrayList<String>();
    for (String pr : providerLocations.keySet()) if (!providers.contains(pr))
        removedProviders.add(pr);
    // remove cached locations and invalidators for providers which are no longer selected
    for (String pr : removedProviders) {
        providerLocations.remove(pr);
        providerInvalidators.remove(pr);
    }
    // ensure there is a cached location for each chosen provider (can be null)
    for (String pr : providers) {
        if ((allProviders.indexOf(pr) >= 0) && !providerLocations.containsKey(pr)) {
            Location location = new Location("");
            providerLocations.put(pr, location);
        }
    }
    // add overlays
    updateLocationProviderStyles();
    mapCircles = new HashMap<String, Circle>();
    mapMarkers = new HashMap<String, Marker>();
    Log.d(TAG, "Provider location cache: " + providerLocations.keySet().toString());
    Layers layers = mapMap.getLayerManager().getLayers();
    // remove all layers other than tile render layer from map
    for (Layer layer : layers) if (!(layer instanceof TileRendererLayer) && !(layer instanceof TileDownloadLayer)) {
        layer.onDestroy();
        layers.remove(layer);
    }
    for (String pr : providers) {
        // no invalidator for GPS, which is invalidated through GPS status
        if ((!pr.equals(LocationManager.GPS_PROVIDER)) && (providerInvalidators.get(pr)) == null) {
            final String provider = pr;
            final Context ctx = context;
            providerInvalidators.put(pr, new Runnable() {

                private String mProvider = provider;

                @Override
                public void run() {
                    Location location = providerLocations.get(mProvider);
                    if (location != null)
                        markLocationAsStale(location);
                    applyLocationProviderStyle(ctx, mProvider, Const.LOCATION_PROVIDER_GRAY);
                }
            });
        }
        String styleName = assignLocationProviderStyle(pr);
        LatLong latLong;
        float acc;
        boolean visible;
        if ((providerLocations.get(pr) != null) && (providerLocations.get(pr).getProvider() != "")) {
            latLong = new LatLong(providerLocations.get(pr).getLatitude(), providerLocations.get(pr).getLongitude());
            if (providerLocations.get(pr).hasAccuracy())
                acc = providerLocations.get(pr).getAccuracy();
            else
                acc = 0;
            visible = true;
            if (isLocationStale(providerLocations.get(pr)))
                styleName = Const.LOCATION_PROVIDER_GRAY;
            Log.d("MainActivity", pr + " has " + latLong.toString());
        } else {
            latLong = new LatLong(0, 0);
            acc = 0;
            visible = false;
            Log.d("MainActivity", pr + " has no location, hiding");
        }
        // Circle layer
        Resources res = context.getResources();
        TypedArray style = res.obtainTypedArray(res.getIdentifier(styleName, "array", context.getPackageName()));
        Paint fill = AndroidGraphicFactory.INSTANCE.createPaint();
        float density = context.getResources().getDisplayMetrics().density;
        fill.setColor(style.getColor(Const.STYLE_FILL, R.color.circle_gray_fill));
        fill.setStyle(Style.FILL);
        Paint stroke = AndroidGraphicFactory.INSTANCE.createPaint();
        stroke.setColor(style.getColor(Const.STYLE_STROKE, R.color.circle_gray_stroke));
        stroke.setStrokeWidth(Math.max(1.5f * density, 1));
        stroke.setStyle(Style.STROKE);
        Circle circle = new Circle(latLong, acc, fill, stroke);
        mapCircles.put(pr, circle);
        layers.add(circle);
        circle.setVisible(visible);
        // Marker layer
        Drawable drawable = style.getDrawable(Const.STYLE_MARKER);
        Bitmap bitmap = AndroidGraphicFactory.convertToBitmap(drawable);
        Marker marker = new Marker(latLong, bitmap, 0, -bitmap.getHeight() * 10 / 24);
        mapMarkers.put(pr, marker);
        layers.add(marker);
        marker.setVisible(visible);
        style.recycle();
    }
    // move layers into view
    updateMap();
}
Also used : Context(android.content.Context) Circle(org.mapsforge.map.layer.overlay.Circle) TileRendererLayer(org.mapsforge.map.layer.renderer.TileRendererLayer) ArrayList(java.util.ArrayList) TileDownloadLayer(org.mapsforge.map.layer.download.TileDownloadLayer) Drawable(android.graphics.drawable.Drawable) Marker(org.mapsforge.map.layer.overlay.Marker) Paint(org.mapsforge.core.graphics.Paint) TileRendererLayer(org.mapsforge.map.layer.renderer.TileRendererLayer) Layer(org.mapsforge.map.layer.Layer) TileDownloadLayer(org.mapsforge.map.layer.download.TileDownloadLayer) Bitmap(org.mapsforge.core.graphics.Bitmap) TypedArray(android.content.res.TypedArray) Resources(android.content.res.Resources) Layers(org.mapsforge.map.layer.Layers) LatLong(org.mapsforge.core.model.LatLong) Location(android.location.Location)

Example 3 with TileRendererLayer

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

TileDownloadLayer (org.mapsforge.map.layer.download.TileDownloadLayer)3 TileRendererLayer (org.mapsforge.map.layer.renderer.TileRendererLayer)3 LatLong (org.mapsforge.core.model.LatLong)2 Layers (org.mapsforge.map.layer.Layers)2 Context (android.content.Context)1 SharedPreferences (android.content.SharedPreferences)1 Resources (android.content.res.Resources)1 TypedArray (android.content.res.TypedArray)1 Drawable (android.graphics.drawable.Drawable)1 Location (android.location.Location)1 Uri (android.net.Uri)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Bitmap (org.mapsforge.core.graphics.Bitmap)1 Paint (org.mapsforge.core.graphics.Paint)1 Point (org.mapsforge.core.model.Point)1 MapDataStore (org.mapsforge.map.datastore.MapDataStore)1 MultiMapDataStore (org.mapsforge.map.datastore.MultiMapDataStore)1 Layer (org.mapsforge.map.layer.Layer)1 LayerManager (org.mapsforge.map.layer.LayerManager)1