Search in sources :

Example 11 with LatLong

use of org.mapsforge.core.model.LatLong 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)

Example 12 with LatLong

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

the class MapSectionFragment method onStop.

@Override
public void onStop() {
    LatLong center = mapMap.getModel().mapViewPosition.getCenter();
    byte zoom = mapMap.getModel().mapViewPosition.getZoomLevel();
    SharedPreferences.Editor spEditor = mainActivity.mSharedPreferences.edit();
    spEditor.putFloat(Const.KEY_PREF_MAP_LAT, (float) center.latitude);
    spEditor.putFloat(Const.KEY_PREF_MAP_LON, (float) center.longitude);
    spEditor.putInt(Const.KEY_PREF_MAP_ZOOM, zoom);
    spEditor.commit();
    super.onStop();
}
Also used : SharedPreferences(android.content.SharedPreferences) LatLong(org.mapsforge.core.model.LatLong)

Aggregations

LatLong (org.mapsforge.core.model.LatLong)12 Location (android.location.Location)6 Point (org.mapsforge.core.model.Point)4 Marker (org.mapsforge.map.layer.overlay.Marker)4 Paint (org.mapsforge.core.graphics.Paint)3 TileDownloadLayer (org.mapsforge.map.layer.download.TileDownloadLayer)3 TileRendererLayer (org.mapsforge.map.layer.renderer.TileRendererLayer)3 Context (android.content.Context)2 Intent (android.content.Intent)2 Drawable (android.graphics.drawable.Drawable)2 Uri (android.net.Uri)2 Bundle (android.os.Bundle)2 View (android.view.View)2 Window (android.view.Window)2 MapDataStore (org.mapsforge.map.datastore.MapDataStore)2 AbstractTileSource (org.mapsforge.map.layer.download.tilesource.AbstractTileSource)2 Circle (org.mapsforge.map.layer.overlay.Circle)2 Manifest (android.Manifest)1 Activity (android.app.Activity)1 SharedPreferences (android.content.SharedPreferences)1