Search in sources :

Example 1 with BoundingBox

use of com.codename1.maps.BoundingBox in project CodenameOne by codenameone.

the class LayerWithZoomLevels method pointerReleased.

/**
 * {@inheritDoc}
 */
public void pointerReleased(int x, int y) {
    super.pointerReleased(x, y);
    final long currTime = System.currentTimeMillis();
    if (currTime - lastPressed < singleTapThreshold) {
        tapCount++;
        final int tapX = x;
        final int tapY = y;
        final int currTapCount = tapCount;
        UITimer timer = new UITimer(new Runnable() {

            public void run() {
                if (currTapCount == tapCount) {
                    pointerTapped(tapX, tapY, tapCount);
                    tapCount = 0;
                }
            }
        });
        timer.schedule(doubleTapThreshold, false, this.getComponentForm());
    } else {
        tapCount = 0;
    }
    if (oldDistance != -1) {
        double scale = (double) scaleX / (double) getWidth();
        Coord refCoord = this.getCoordFromPosition(zoomCenterX + getAbsoluteX(), zoomCenterY + getAbsoluteY());
        int oldZoom = _zoom;
        if (scale > 1) {
            if (scale < 1.2) {
            // do nothing
            } else if (scale < 1.6) {
                zoomIn();
            } else if (scale < 2.0) {
                zoomIn();
                zoomIn();
            } else if (scale < 2.4) {
                zoomIn();
                zoomIn();
                zoomIn();
            } else {
                zoomIn();
                zoomIn();
                zoomIn();
                zoomIn();
            }
        } else {
            if (scale > 0.8) {
            // do nothing
            } else if (scale > 0.5) {
                zoomOut();
            } else if (scale > 0.2) {
                zoomOut();
                zoomOut();
            } else {
                zoomOut();
                zoomOut();
                zoomOut();
            }
        }
        if (oldZoom != _zoom) {
            Coord c1 = this.getCoordFromPosition(0, 0);
            Coord c2 = this.getCoordFromPosition(getWidth(), getHeight());
            Coord pixelToCoord = new Coord((c2.getLatitude() - c1.getLatitude()) / (float) getHeight(), (c2.getLongitude() - c1.getLongitude()) / (float) getWidth());
            float offX = (getWidth() / 2) - zoomCenterX;
            float offY = (getHeight() / 2) - zoomCenterY;
            _center = _map.projection().fromWGS84(refCoord.translate(offY * pixelToCoord.getLatitude(), offX * pixelToCoord.getLongitude()));
        }
        translateX = 0;
        translateY = 0;
        scaleX = 0;
        scaleY = 0;
        oldDistance = -1;
        if (buffer != null) {
            // buffer.dispose();
            buffer = null;
            refreshLayers = true;
        }
        if (Display.getInstance().areMutableImagesFast()) {
            super.repaint();
        } else {
            // workaround for rounding error in scale/clipping
            getComponentForm().repaint();
        }
        fireMapListenerEvent();
        return;
    }
    Coord scale = _map.scale(_zoom);
    _center = _center.translate(translateY * -scale.getLatitude(), translateX * scale.getLongitude());
    _needTiles = true;
    translateX = 0;
    translateY = 0;
    x = x - getAbsoluteX();
    y = y - getAbsoluteY();
    Tile t = screenTile();
    Coord southWest = t.position(x - 20, t.dimension().getHeight() - y - 20);
    Coord c = Mercator.inverseMercator(southWest.getLatitude(), southWest.getLongitude());
    Coord northEast = t.position(x + 20, t.dimension().getHeight() - y + 20);
    c = Mercator.inverseMercator(northEast.getLatitude(), northEast.getLongitude());
    BoundingBox bbox = new BoundingBox(southWest, northEast);
    Enumeration e = _layers.elements();
    while (e.hasMoreElements()) {
        LayerWithZoomLevels layer = (LayerWithZoomLevels) e.nextElement();
        if (layer.layer instanceof PointsLayer) {
            ((PointsLayer) layer.layer).fireActionEvent(bbox);
        }
    }
    super.repaint();
    fireMapListenerEvent();
}
Also used : PointsLayer(com.codename1.maps.layers.PointsLayer) Enumeration(java.util.Enumeration) UITimer(com.codename1.ui.util.UITimer) Point(com.codename1.ui.geom.Point)

Example 2 with BoundingBox

use of com.codename1.maps.BoundingBox in project CodenameOne by codenameone.

the class LayerWithZoomLevels method zoomTo.

/**
 * Zoom the map the the giving bounding box
 *
 * @param boundingBox to zoom to
 * @throws IllegalArgumentException if the boundingBox is not wg84 format
 */
public void zoomTo(BoundingBox boundingBox) {
    if (boundingBox.projected()) {
        throw new IllegalArgumentException("boundingBox should be wg84 format");
    }
    Dimension dimension = null;
    if (getWidth() == 0 || getHeight() == 0) {
        dimension = getPreferredSize();
    } else {
        dimension = new Dimension(getWidth(), getHeight());
    }
    final BoundingBox projectedBBOX = _map.projection().fromWGS84(boundingBox);
    Tile tile = new Tile(dimension, projectedBBOX, null);
    _zoom = _map.maxZoomFor(tile);
    _center = tile.position(tile.dimension().getWidth() / 2, tile.dimension().getHeight() / 2);
    _needTiles = true;
    super.repaint();
}
Also used : Dimension(com.codename1.ui.geom.Dimension)

Example 3 with BoundingBox

use of com.codename1.maps.BoundingBox in project codenameone-google-maps by codenameone.

the class MapContainer method fitBounds.

/**
 * Pans and zooms to fit the given bounding box.
 * @param bounds The bounding box to display.
 */
public void fitBounds(BoundingBox bounds) {
    Coord c = new Coord((bounds.getNorthEast().getLatitude() + bounds.getSouthWest().getLatitude()) / 2, (bounds.getNorthEast().getLongitude() + bounds.getSouthWest().getLongitude()) / 2);
    double currZoom = getZoom();
    BoundingBox currBbox = getBoundingBox();
    Coord currC = new Coord((currBbox.getNorthEast().getLatitude() + currBbox.getSouthWest().getLatitude()) / 2, (currBbox.getNorthEast().getLongitude() + currBbox.getSouthWest().getLongitude()) / 2);
    double currMetersPerPx = 156543.03392 * Math.cos(currC.getLatitude() * Math.PI / 180) / MathUtil.pow(2, currZoom);
    double targetMetersPerPx = 156543.03392 * Math.cos(c.getLatitude() * Math.PI / 180) / MathUtil.pow(2, currZoom);
    double adjustmentFactor = targetMetersPerPx / currMetersPerPx;
    // Log.p("Adjustment factor ="+adjustmentFactor);
    Mercator proj = new Mercator();
    BoundingBox currProjected = proj.fromWGS84(currBbox);
    BoundingBox targetProjected = proj.fromWGS84(bounds);
    double zoom = currZoom;
    double currLatDiff = Math.abs(currProjected.latitudeDifference());
    double currLngDiff = Math.abs(currProjected.longitudeDifference());
    if (currLatDiff == 0) {
        currLatDiff = currMetersPerPx * getHeight();
    }
    if (currLatDiff == 0) {
        currLatDiff = currMetersPerPx * Display.getInstance().getDisplayHeight();
    }
    if (currLngDiff == 0) {
        currLngDiff = currMetersPerPx * getWidth();
    }
    if (currLngDiff == 0) {
        currLngDiff = currMetersPerPx * Display.getInstance().getDisplayWidth();
    }
    double targetLatDiff = Math.max(Math.abs(targetProjected.latitudeDifference()), 0.0001);
    double targetLngDiff = Math.max(Math.abs(targetProjected.longitudeDifference()), 0.0001);
    double latDiff = currLatDiff;
    double lngDiff = currLngDiff;
    while (targetLatDiff < latDiff && targetLngDiff * adjustmentFactor < lngDiff) {
        zoom += 1.0;
        latDiff /= 2.0;
        lngDiff /= 2.0;
    }
    // Log.p("Finished zooming in");
    while (targetLatDiff > latDiff || targetLngDiff * adjustmentFactor > lngDiff) {
        zoom -= 1.0;
        latDiff *= 2.0;
        lngDiff *= 2.0;
    // Log.p("latDiff now="+latDiff+", lngDiff now = "+lngDiff);
    }
    // Log.p("Finished zooming out");
    // Log.p("After: latDiff="+latDiff+", lngDiff="+lngDiff+", zoom="+Math.floor(zoom));
    zoom(c, (int) Math.floor(zoom));
// setCameraPosition(c);
// Log.p("Setting center to "+c);
// Log.p("In order to fit bounds "+bounds);
/*
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                Display.getInstance().callSerially(new Runnable() {
                    public void run() {
                        Coord center = MapContainer.this.getCameraPosition();
                        Log.p("New Center is "+center);
                        Log.p("New bbox is "+getBoundingBox());
                    }
                });
            }

        }, 1000);
        */
}
Also used : Coord(com.codename1.maps.Coord) Mercator(com.codename1.maps.Mercator) BoundingBox(com.codename1.maps.BoundingBox)

Example 4 with BoundingBox

use of com.codename1.maps.BoundingBox in project CodenameOne by codenameone.

the class TiledProvider method bboxFor.

/**
 * {@inheritDoc}
 */
public BoundingBox bboxFor(Coord position, int zoomLevel) {
    _zoomLevel = zoomLevel;
    Coord scale = scale(zoomLevel);
    Dimension tileSize = tileSize();
    double x = scale.getLongitude() * tileSize.getWidth();
    double y = scale.getLatitude() * tileSize.getHeight();
    Coord tileScale = new Coord(y, x, false);
    _tileNo = tileNo(position, projection().extent().getSouthWest(), tileScale);
    Coord start = tileCoord(_tileNo, projection().extent().getSouthWest(), tileScale);
    Coord end = start.translate(tileScale.getLatitude(), tileScale.getLongitude());
    return new BoundingBox(start, end);
}
Also used : Coord(com.codename1.maps.Coord) BoundingBox(com.codename1.maps.BoundingBox) Dimension(com.codename1.ui.geom.Dimension)

Example 5 with BoundingBox

use of com.codename1.maps.BoundingBox in project CodenameOne by codenameone.

the class GoogleMapsProvider method tileFor.

/**
 * {@inheritDoc}
 */
public Tile tileFor(BoundingBox bbox) {
    StringBuilder sb = new StringBuilder(_url);
    Coord ne = bbox.getNorthEast();
    Coord c = projection().toWGS84(new Coord(ne.getLatitude() - bbox.latitudeDifference() / 2, ne.getLongitude() - bbox.longitudeDifference() / 2, true));
    sb.append("center=");
    sb.append(c.getLatitude());
    sb.append(",");
    sb.append(c.getLongitude());
    sb.append("&format=png");
    sb.append("&zoom=" + _zoomLevel);
    sb.append("&size=");
    sb.append(tileSize);
    sb.append("x");
    sb.append(tileSize);
    sb.append("&sensor=");
    sb.append(sensor);
    if (language != null) {
        sb.append("&language=");
        sb.append(language);
    }
    if (type == SATELLITE) {
        sb.append("&maptype=satellite");
    } else if (type == HYBRID) {
        sb.append("&maptype=hybrid");
    }
    sb.append("&key=" + apiKey);
    return new ProxyHttpTile(tileSize(), bbox, sb.toString());
}
Also used : Coord(com.codename1.maps.Coord) ProxyHttpTile(com.codename1.maps.ProxyHttpTile)

Aggregations

BoundingBox (com.codename1.maps.BoundingBox)7 Coord (com.codename1.maps.Coord)7 Dimension (com.codename1.ui.geom.Dimension)3 Point (com.codename1.ui.geom.Point)3 Mercator (com.codename1.maps.Mercator)2 FloatingActionButton (com.codename1.components.FloatingActionButton)1 InteractionDialog (com.codename1.components.InteractionDialog)1 ToastBar (com.codename1.components.ToastBar)1 MapContainer (com.codename1.googlemaps.MapContainer)1 MapObject (com.codename1.googlemaps.MapContainer.MapObject)1 Util (com.codename1.io.Util)1 MapListener (com.codename1.maps.MapListener)1 ProxyHttpTile (com.codename1.maps.ProxyHttpTile)1 PointsLayer (com.codename1.maps.layers.PointsLayer)1 Button (com.codename1.ui.Button)1 Component (com.codename1.ui.Component)1 ComponentSelector.$ (com.codename1.ui.ComponentSelector.$)1 Container (com.codename1.ui.Container)1 Display (com.codename1.ui.Display)1 EncodedImage (com.codename1.ui.EncodedImage)1