Search in sources :

Example 6 with Bitmap

use of org.mapsforge.core.graphics.Bitmap 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)

Aggregations

Bitmap (org.mapsforge.core.graphics.Bitmap)6 Drawable (android.graphics.drawable.Drawable)4 MarkerBitmap (de.bahnhoefe.deutschlands.bahnhofsfotos.mapsforge.MarkerBitmap)4 Marker (org.mapsforge.map.layer.overlay.Marker)4 Context (android.content.Context)2 Resources (android.content.res.Resources)2 TypedArray (android.content.res.TypedArray)2 Location (android.location.Location)2 ArrayList (java.util.ArrayList)2 Paint (org.mapsforge.core.graphics.Paint)2 Manifest (android.Manifest)1 Activity (android.app.Activity)1 Intent (android.content.Intent)1 PackageManager (android.content.pm.PackageManager)1 Color (android.graphics.Color)1 LocationListener (android.location.LocationListener)1 LocationManager (android.location.LocationManager)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 VibrationEffect (android.os.VibrationEffect)1