Search in sources :

Example 1 with Point

use of org.mapsforge.core.model.Point in project RSAndroidApp by RailwayStations.

the class MapsActivity method onLongPress.

private void onLongPress(final LatLong tapLatLong) {
    if (missingMarker == null) {
        // marker to show at the location
        final Drawable drawable = ContextCompat.getDrawable(this, R.drawable.marker_missing);
        assert drawable != null;
        final Bitmap bitmap = AndroidGraphicFactory.convertToBitmap(drawable);
        missingMarker = new Marker(tapLatLong, bitmap, -(bitmap.getWidth() / 2), -bitmap.getHeight()) {

            @Override
            public boolean onTap(final LatLong tapLatLong, final Point layerXY, final Point tapXY) {
                new SimpleDialogs().confirm(MapsActivity.this, R.string.add_missing_station, (dialogInterface, i) -> {
                    final Intent intent = new Intent(MapsActivity.this, DetailsActivity.class);
                    intent.putExtra(DetailsActivity.EXTRA_LATITUDE, getLatLong().latitude);
                    intent.putExtra(DetailsActivity.EXTRA_LONGITUDE, getLatLong().longitude);
                    startActivity(intent);
                });
                return false;
            }
        };
        binding.map.mapView.getLayerManager().getLayers().add(missingMarker);
    } else {
        missingMarker.setLatLong(tapLatLong);
        missingMarker.requestRedraw();
    }
    // feedback for long click
    ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(150, VibrationEffect.DEFAULT_AMPLITUDE));
}
Also used : Bundle(android.os.Bundle) PackageManager(android.content.pm.PackageManager) NonNull(androidx.annotation.NonNull) StationFilter(de.bahnhoefe.deutschlands.bahnhofsfotos.util.StationFilter) Uri(android.net.Uri) WindowManager(android.view.WindowManager) LocationListener(android.location.LocationListener) Drawable(android.graphics.drawable.Drawable) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) Align(org.mapsforge.core.graphics.Align) GeoItem(de.bahnhoefe.deutschlands.bahnhofsfotos.mapsforge.GeoItem) NONE(android.view.Menu.NONE) Manifest(android.Manifest) CheckBox(android.widget.CheckBox) Map(java.util.Map) AndroidGraphicFactory(org.mapsforge.map.android.graphics.AndroidGraphicFactory) TileRendererLayer(org.mapsforge.map.layer.renderer.TileRendererLayer) View(android.view.View) IMapViewPosition(org.mapsforge.map.model.IMapViewPosition) ContextCompat(androidx.core.content.ContextCompat) Log(android.util.Log) Station(de.bahnhoefe.deutschlands.bahnhofsfotos.model.Station) MapFile(org.mapsforge.map.reader.MapFile) DbAdapter(de.bahnhoefe.deutschlands.bahnhofsfotos.db.DbAdapter) Layer(org.mapsforge.map.layer.Layer) SubMenu(android.view.SubMenu) MapPosition(org.mapsforge.core.model.MapPosition) XmlRenderTheme(org.mapsforge.map.rendertheme.XmlRenderTheme) FileNotFoundException(java.io.FileNotFoundException) MapZoomControls(org.mapsforge.map.android.input.MapZoomControls) AbstractTileSource(org.mapsforge.map.layer.download.tilesource.AbstractTileSource) List(java.util.List) DocumentFile(androidx.documentfile.provider.DocumentFile) Marker(org.mapsforge.map.layer.overlay.Marker) ActivityResultContracts(androidx.activity.result.contract.ActivityResultContracts) TileCache(org.mapsforge.map.layer.cache.TileCache) Location(android.location.Location) LocationManager(android.location.LocationManager) Window(android.view.Window) Context(android.content.Context) ResourcesCompat(androidx.core.content.res.ResourcesCompat) Intent(android.content.Intent) FontStyle(org.mapsforge.core.graphics.FontStyle) HashMap(java.util.HashMap) TapHandler(de.bahnhoefe.deutschlands.bahnhofsfotos.mapsforge.TapHandler) Point(org.mapsforge.core.model.Point) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) VibrationEffect(android.os.VibrationEffect) Bitmap(org.mapsforge.core.graphics.Bitmap) InternalRenderTheme(org.mapsforge.map.rendertheme.InternalRenderTheme) Toast(android.widget.Toast) FontFamily(org.mapsforge.core.graphics.FontFamily) Menu(android.view.Menu) StreamRenderTheme(org.mapsforge.map.rendertheme.StreamRenderTheme) DbsTileSource(de.bahnhoefe.deutschlands.bahnhofsfotos.mapsforge.DbsTileSource) MarkerBitmap(de.bahnhoefe.deutschlands.bahnhofsfotos.mapsforge.MarkerBitmap) WeakReference(java.lang.ref.WeakReference) AndroidUtil(org.mapsforge.map.android.util.AndroidUtil) OnlineTileSource(org.mapsforge.map.layer.download.tilesource.OnlineTileSource) ActivityResultLauncher(androidx.activity.result.ActivityResultLauncher) ActivityCompat(androidx.core.app.ActivityCompat) TileDownloadLayer(org.mapsforge.map.layer.download.TileDownloadLayer) FileInputStream(java.io.FileInputStream) StationFilterBar(de.bahnhoefe.deutschlands.bahnhofsfotos.dialogs.StationFilterBar) LatLong(org.mapsforge.core.model.LatLong) ActivityMapsBinding(de.bahnhoefe.deutschlands.bahnhofsfotos.databinding.ActivityMapsBinding) MapDataStore(org.mapsforge.map.datastore.MapDataStore) ClusterManager(de.bahnhoefe.deutschlands.bahnhofsfotos.mapsforge.ClusterManager) MapInfoFragment(de.bahnhoefe.deutschlands.bahnhofsfotos.dialogs.MapInfoFragment) Color(android.graphics.Color) Paint(org.mapsforge.core.graphics.Paint) Upload(de.bahnhoefe.deutschlands.bahnhofsfotos.model.Upload) SimpleDialogs(de.bahnhoefe.deutschlands.bahnhofsfotos.dialogs.SimpleDialogs) Vibrator(android.os.Vibrator) Style(org.mapsforge.core.graphics.Style) OpenStreetMapMapnik(org.mapsforge.map.layer.download.tilesource.OpenStreetMapMapnik) Activity(android.app.Activity) Bitmap(org.mapsforge.core.graphics.Bitmap) MarkerBitmap(de.bahnhoefe.deutschlands.bahnhofsfotos.mapsforge.MarkerBitmap) SimpleDialogs(de.bahnhoefe.deutschlands.bahnhofsfotos.dialogs.SimpleDialogs) Drawable(android.graphics.drawable.Drawable) Intent(android.content.Intent) Marker(org.mapsforge.map.layer.overlay.Marker) Point(org.mapsforge.core.model.Point) Vibrator(android.os.Vibrator) LatLong(org.mapsforge.core.model.LatLong)

Example 2 with Point

use of org.mapsforge.core.model.Point 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 3 with Point

use of org.mapsforge.core.model.Point in project RSAndroidApp by RailwayStations.

the class MapsActivity method getMarkerBitmap.

private List<MarkerBitmap> getMarkerBitmap() {
    final List<MarkerBitmap> markerBitmaps = new ArrayList<>();
    // prepare for marker icons.
    // small icon for maximum single item
    final Bitmap bitmapWithPhoto = loadBitmap(R.drawable.marker_green);
    final Bitmap markerWithoutPhoto = loadBitmap(R.drawable.marker_red);
    final Bitmap markerOwnPhoto = loadBitmap(R.drawable.marker_violet);
    final Bitmap markerPendingUpload = loadBitmap(R.drawable.marker_yellow);
    final Bitmap markerWithPhotoInactive = loadBitmap(R.drawable.marker_green_inactive);
    final Bitmap markerWithoutPhotoInactive = loadBitmap(R.drawable.marker_red_inactive);
    final Bitmap markerOwnPhotoInactive = loadBitmap(R.drawable.marker_violet_inactive);
    final Paint paint1 = AndroidGraphicFactory.INSTANCE.createPaint();
    paint1.setStyle(Style.FILL);
    paint1.setTextAlign(Align.CENTER);
    FontFamily fontFamily = FontFamily.DEFAULT;
    FontStyle fontStyle = FontStyle.BOLD;
    paint1.setTypeface(fontFamily, fontStyle);
    paint1.setColor(Color.RED);
    markerBitmaps.add(new MarkerBitmap(this.getApplicationContext(), markerWithoutPhoto, bitmapWithPhoto, markerOwnPhoto, markerWithoutPhotoInactive, markerWithPhotoInactive, markerOwnPhotoInactive, markerPendingUpload, new Point(0, -(markerWithoutPhoto.getHeight() / 2.0)), 10f, 1, paint1));
    // small cluster icon. for 10 or less items.
    final Bitmap bitmapBalloonSN = loadBitmap(R.drawable.balloon_s_n);
    final Paint paint2 = AndroidGraphicFactory.INSTANCE.createPaint();
    paint2.setStyle(Style.FILL);
    paint2.setTextAlign(Align.CENTER);
    fontFamily = FontFamily.DEFAULT;
    fontStyle = FontStyle.BOLD;
    paint2.setTypeface(fontFamily, fontStyle);
    paint2.setColor(Color.BLACK);
    markerBitmaps.add(new MarkerBitmap(this.getApplicationContext(), bitmapBalloonSN, new Point(0, 0), 9f, 10, paint2));
    // large cluster icon. 100 will be ignored.
    final Bitmap bitmapBalloonMN = loadBitmap(R.drawable.balloon_m_n);
    final Paint paint3 = AndroidGraphicFactory.INSTANCE.createPaint();
    paint3.setStyle(Style.FILL);
    paint3.setTextAlign(Align.CENTER);
    fontFamily = FontFamily.DEFAULT;
    fontStyle = FontStyle.BOLD;
    paint3.setTypeface(fontFamily, fontStyle);
    paint3.setColor(Color.BLACK);
    markerBitmaps.add(new MarkerBitmap(this.getApplicationContext(), bitmapBalloonMN, new Point(0, 0), 11f, 100, paint3));
    return markerBitmaps;
}
Also used : FontFamily(org.mapsforge.core.graphics.FontFamily) Bitmap(org.mapsforge.core.graphics.Bitmap) MarkerBitmap(de.bahnhoefe.deutschlands.bahnhofsfotos.mapsforge.MarkerBitmap) FontStyle(org.mapsforge.core.graphics.FontStyle) ArrayList(java.util.ArrayList) MarkerBitmap(de.bahnhoefe.deutschlands.bahnhofsfotos.mapsforge.MarkerBitmap) Paint(org.mapsforge.core.graphics.Paint) Point(org.mapsforge.core.model.Point)

Example 4 with Point

use of org.mapsforge.core.model.Point in project RSAndroidApp by RailwayStations.

the class ClusterMarker method draw.

@Override
public synchronized void draw(final BoundingBox boundingBox, final byte zoomLevel, final org.mapsforge.core.graphics.Canvas canvas, final Point topLeftPoint) {
    if (cluster.getClusterManager() == null || this.getLatLong() == null) {
        return;
    }
    setMarkerBitmap();
    final long mapSize = MercatorProjection.getMapSize(zoomLevel, this.displayModel.getTileSize());
    final double pixelX = MercatorProjection.longitudeToPixelX(this.getLatLong().longitude, mapSize);
    final double pixelY = MercatorProjection.latitudeToPixelY(this.getLatLong().latitude, mapSize);
    final double halfBitmapWidth;
    final double halfBitmapHeight;
    final var markerBitmap = cluster.getClusterManager().markerIconBmps.get(markerType);
    final var bitmap = markerBitmap.getBitmap(hasPhoto(), ownPhoto(), stationActive(), isPendingUpload());
    try {
        halfBitmapWidth = bitmap.getWidth() / 2f;
        halfBitmapHeight = bitmap.getHeight() / 2f;
    } catch (final NullPointerException e) {
        Log.e(ClusterMarker.TAG, e.getMessage(), e);
        return;
    }
    final int left = (int) (pixelX - topLeftPoint.x - halfBitmapWidth + markerBitmap.getIconOffset().x);
    final int top = (int) (pixelY - topLeftPoint.y - halfBitmapHeight + markerBitmap.getIconOffset().y);
    final int right = (left + bitmap.getWidth());
    final int bottom = (top + bitmap.getHeight());
    final var bitmapRectangle = new Rectangle(left, top, right, bottom);
    final var canvasRectangle = new Rectangle(0, 0, canvas.getWidth(), canvas.getHeight());
    if (!canvasRectangle.intersects(bitmapRectangle)) {
        return;
    }
    // Draw bitmap
    canvas.drawBitmap(bitmap, left, top);
    // Draw Text
    if (markerType == 0) {
        // Draw bitmap
        final var bubble = MarkerBitmap.getBitmapFromTitle(cluster.getTitle(), markerBitmap.getPaint());
        canvas.drawBitmap(bubble, (int) (left + halfBitmapWidth - bubble.getWidth() / 2), (top - bubble.getHeight()));
    } else {
        final int x = (int) (left + bitmap.getWidth() * 1.3);
        final int y = (int) (top + bitmap.getHeight() * 1.3 + markerBitmap.getPaint().getTextHeight(cluster.getTitle()) / 2);
        canvas.drawText(cluster.getTitle(), x, y, markerBitmap.getPaint());
    }
}
Also used : Rectangle(org.mapsforge.core.model.Rectangle) Point(org.mapsforge.core.model.Point)

Example 5 with Point

use of org.mapsforge.core.model.Point in project satstat by mvglasow.

the class MapSectionFragment method updateMap.

/**
 * Updates the map view so that all markers are visible.
 */
public void updateMap() {
    boolean needsRedraw = false;
    Dimension dimension = mapMap.getModel().mapViewDimension.getDimension();
    // just trigger a redraw if we're not going to pan or zoom
    if ((dimension == null) || (!isMapViewAttached)) {
        mapMap.getLayerManager().redrawLayers();
        return;
    }
    // move locations into view and zoom out as needed
    int tileSize = mapMap.getModel().displayModel.getTileSize();
    BoundingBox bb = null;
    BoundingBox bb2 = null;
    for (Location l : providerLocations.values()) if ((l != null) && (l.getProvider() != "")) {
        double lat = l.getLatitude();
        double lon = l.getLongitude();
        double yRadius = l.hasAccuracy() ? ((l.getAccuracy() * 360.0f) / Const.EARTH_CIRCUMFERENCE) : 0;
        double xRadius = l.hasAccuracy() ? (yRadius * Math.abs(Math.cos(lat))) : 0;
        double minLon = Math.max(lon - xRadius, -180);
        double maxLon = Math.min(lon + xRadius, 180);
        double minLat = Math.max(lat - yRadius, -90);
        double maxLat = Math.min(lat + yRadius, 90);
        if (!isLocationStale(l)) {
            // location is up to date, add to main BoundingBox
            if (bb != null) {
                minLat = Math.min(bb.minLatitude, minLat);
                maxLat = Math.max(bb.maxLatitude, maxLat);
                minLon = Math.min(bb.minLongitude, minLon);
                maxLon = Math.max(bb.maxLongitude, maxLon);
            }
            bb = new BoundingBox(minLat, minLon, maxLat, maxLon);
        } else {
            // location is stale, add to stale BoundingBox
            if (bb2 != null) {
                minLat = Math.min(bb2.minLatitude, minLat);
                maxLat = Math.max(bb2.maxLatitude, maxLat);
                minLon = Math.min(bb2.minLongitude, minLon);
                maxLon = Math.max(bb2.maxLongitude, maxLon);
            }
            bb2 = new BoundingBox(minLat, minLon, maxLat, maxLon);
        }
    }
    // all locations are stale, center to them
    if (bb == null)
        bb = bb2;
    if (bb == null) {
        needsRedraw = true;
    } else {
        byte newZoom = LatLongUtils.zoomForBounds(dimension, bb, tileSize);
        if (newZoom < 0)
            newZoom = 0;
        if (newZoom < mapMap.getModel().mapViewPosition.getZoomLevel()) {
            mapMap.getModel().mapViewPosition.setZoomLevel(newZoom);
        } else {
            needsRedraw = true;
        }
        MapViewProjection proj = new MapViewProjection(mapMap);
        Point nw = proj.toPixels(new LatLong(bb.maxLatitude, bb.minLongitude));
        Point se = proj.toPixels(new LatLong(bb.minLatitude, bb.maxLongitude));
        // move only if bb is not entirely visible
        if ((nw.x < 0) || (nw.y < 0) || (se.x > dimension.width) || (se.y > dimension.height)) {
            mapMap.getModel().mapViewPosition.setCenter(bb.getCenterPoint());
        } else {
            needsRedraw = true;
        }
    }
    if (needsRedraw)
        mapMap.getLayerManager().redrawLayers();
}
Also used : MapViewProjection(org.mapsforge.map.util.MapViewProjection) BoundingBox(org.mapsforge.core.model.BoundingBox) Dimension(org.mapsforge.core.model.Dimension) Point(org.mapsforge.core.model.Point) LatLong(org.mapsforge.core.model.LatLong) Point(org.mapsforge.core.model.Point) Paint(org.mapsforge.core.graphics.Paint) Location(android.location.Location)

Aggregations

Point (org.mapsforge.core.model.Point)5 LatLong (org.mapsforge.core.model.LatLong)3 Location (android.location.Location)2 Uri (android.net.Uri)2 Paint (org.mapsforge.core.graphics.Paint)2 MapDataStore (org.mapsforge.map.datastore.MapDataStore)2 TileDownloadLayer (org.mapsforge.map.layer.download.TileDownloadLayer)2 AbstractTileSource (org.mapsforge.map.layer.download.tilesource.AbstractTileSource)2 TileRendererLayer (org.mapsforge.map.layer.renderer.TileRendererLayer)2 Manifest (android.Manifest)1 Activity (android.app.Activity)1 Context (android.content.Context)1 Intent (android.content.Intent)1 PackageManager (android.content.pm.PackageManager)1 Color (android.graphics.Color)1 Drawable (android.graphics.drawable.Drawable)1 LocationListener (android.location.LocationListener)1 LocationManager (android.location.LocationManager)1 Bundle (android.os.Bundle)1 VibrationEffect (android.os.VibrationEffect)1