Search in sources :

Example 1 with PointsLayer

use of com.codename1.maps.layers.PointsLayer in project CodenameOne by codenameone.

the class PointsLayer method addPoint.

/**
 * Adds a point to the PointsLayer
 *
 * @param point a point to add
 */
public void addPoint(PointLayer point) {
    Image pointIcon = point.getIcon();
    if (pointIcon == null) {
        point.setIcon(icon);
    }
    if (!point.isProjected()) {
        Coord c = getProjection().fromWGS84(point);
        point.setLatitude(c.getLatitude());
        point.setLongitude(c.getLongitude());
        point.setProjected(true);
    }
    points.addElement(point);
}
Also used : Coord(com.codename1.maps.Coord) Image(com.codename1.ui.Image)

Example 2 with PointsLayer

use of com.codename1.maps.layers.PointsLayer 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 3 with PointsLayer

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

the class MapContainer method addMarker.

/**
 * Adds a marker to the map with the given attributes
 * @param opts The marker options.
 * @return marker reference object that should be used when removing the marker
 */
public MapObject addMarker(MarkerOptions opts) {
    // public MapObject addMarker(EncodedImage icon, Coord location, String text, String longText, final ActionListener onClick) {
    EncodedImage icon = opts.getIcon();
    Coord location = opts.getLocation();
    String text = opts.getText();
    String longText = opts.getLongText();
    ActionListener onClick = opts.getOnClick();
    if (internalNative != null) {
        byte[] iconData = null;
        if (icon != null) {
            iconData = icon.getImageData();
            internalNative.setMarkerSize(icon.getWidth(), icon.getHeight());
        }
        long key = internalNative.addMarker(iconData, location.getLatitude(), location.getLongitude(), text, longText, onClick != null, opts.anchorU, opts.anchorV);
        MapObject o = new MapObject();
        o.mapKey = key;
        o.callback = onClick;
        markers.add(o);
        return o;
    } else {
        if (internalLightweightCmp != null) {
            PointLayer pl = new PointLayer(location, text, icon);
            if (points == null) {
                points = new PointsLayer();
                internalLightweightCmp.addLayer(points);
            }
            points.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                    PointLayer point = (PointLayer) evt.getSource();
                    for (MapObject o : markers) {
                        if (o.point == point) {
                            if (o.callback != null) {
                                o.callback.actionPerformed(new ActionEvent(o));
                            }
                            evt.consume();
                            return;
                        }
                    }
                }
            });
            points.addPoint(pl);
            MapObject o = new MapObject();
            o.point = pl;
            o.callback = onClick;
            markers.add(o);
            internalLightweightCmp.revalidate();
            return o;
        } else {
            String uri = null;
            int iconWidth = 0;
            int iconHeight = 0;
            int anchorU = 0;
            int anchorV = 0;
            if (icon != null) {
                uri = WebBrowser.createDataURI(icon.getImageData(), "image/png");
                iconWidth = icon.getWidth();
                iconHeight = icon.getHeight();
                anchorU = (int) (icon.getWidth() * opts.anchorU);
                anchorV = (int) (icon.getHeight() * opts.anchorV);
            }
            // browserBridge.waitForReady();
            MapObject o = new MapObject();
            o.callback = onClick;
            o.pending = true;
            final String fUri = uri;
            final int fIconWidth = iconWidth;
            final int fIconHeight = iconHeight;
            final float fAnchorU = anchorU;
            final float fAnchorV = anchorV;
            browserBridge.ready(() -> {
                internalBrowser.execute("callback.onSuccess(" + BRIDGE + ".addMarker(${0}, ${1}, ${2}, ${3}, ${4}, ${5}, ${6}, ${7}, ${8}))", new Object[] { // long key = ((Double)browserBridge.bridge.call("addMarker", new Object[]{
                fUri, location.getLatitude(), location.getLongitude(), text, longText, fIconWidth, fIconHeight, fAnchorU, fAnchorV }, jsres -> {
                    o.mapKey = jsres.getInt();
                    o.pending = false;
                });
            });
            // MapObject o = new MapObject();
            // o.mapKey = res.getInt();
            // o.callback = onClick;
            markers.add(o);
            // System.out.println("MapKey added "+o.mapKey);
            return o;
        }
    }
}
Also used : Coord(com.codename1.maps.Coord) PointsLayer(com.codename1.maps.layers.PointsLayer) ActionListener(com.codename1.ui.events.ActionListener) PointLayer(com.codename1.maps.layers.PointLayer) ActionEvent(com.codename1.ui.events.ActionEvent) EncodedImage(com.codename1.ui.EncodedImage) Point(com.codename1.ui.geom.Point)

Example 4 with PointsLayer

use of com.codename1.maps.layers.PointsLayer in project CodenameOne by codenameone.

the class PointsLayer method removePoint.

/**
 * Removes a point from the PointsLayer
 *
 * @param point to remove from the PointsLayer
 */
public void removePoint(PointLayer point) {
    if (!point.isProjected()) {
        Coord c = getProjection().fromWGS84(point);
        point.setLatitude(c.getLatitude());
        point.setLongitude(c.getLongitude());
        point.setProjected(true);
    }
    points.removeElement(point);
}
Also used : Coord(com.codename1.maps.Coord)

Aggregations

Coord (com.codename1.maps.Coord)3 PointsLayer (com.codename1.maps.layers.PointsLayer)2 Point (com.codename1.ui.geom.Point)2 PointLayer (com.codename1.maps.layers.PointLayer)1 EncodedImage (com.codename1.ui.EncodedImage)1 Image (com.codename1.ui.Image)1 ActionEvent (com.codename1.ui.events.ActionEvent)1 ActionListener (com.codename1.ui.events.ActionListener)1 UITimer (com.codename1.ui.util.UITimer)1 Enumeration (java.util.Enumeration)1