Search in sources :

Example 21 with Coord

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

the class MapContainer method getBoundingBox.

public BoundingBox getBoundingBox() {
    Coord sw = this.getCoordAtPosition(0, getHeight());
    Coord ne = this.getCoordAtPosition(getWidth(), 0);
    return new BoundingBox(sw, ne);
}
Also used : Coord(com.codename1.maps.Coord) BoundingBox(com.codename1.maps.BoundingBox)

Example 22 with Coord

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

the class MapContainer method getScreenCoordinates.

/**
 * Returns the screen points for a list of coordinates.  This is likely more efficient
 * than calling {@link #getScreenCoordinate(com.codename1.maps.Coord) } for each coordinate
 * in the list because this only involves a single call to the native layer.
 * @param coords The coordinates to convert to points.
 * @return A list of points relative to (0,0) of the map container.
 */
public List<Point> getScreenCoordinates(List<Coord> coords) {
    List<Point> out = new ArrayList<Point>(coords.size());
    BoundingBox bbox = getBoundingBox();
    Mercator proj = new Mercator();
    BoundingBox projectedBox = proj.fromWGS84(bbox);
    for (Coord crd : coords) {
        Coord projectedCrd = proj.fromWGS84(crd);
        Point p;
        if (getWidth() <= 0 || getHeight() <= 0) {
            p = new Point(-100, -100);
        } else {
            // Point p = map.getScreenCoordinate(crd);
            double projectedWidth = projectedBox.longitudeDifference();
            double projectedHeight = projectedBox.latitudeDifference();
            double xCoord = (projectedCrd.getLongitude() - projectedBox.getSouthWest().getLongitude()) / projectedWidth * getWidth();
            double yCoord = (projectedBox.getNorthEast().getLatitude() - projectedCrd.getLatitude()) / projectedHeight * getHeight();
            p = new Point((int) xCoord, (int) yCoord);
        }
        out.add(p);
    }
    return out;
}
Also used : Coord(com.codename1.maps.Coord) Mercator(com.codename1.maps.Mercator) BoundingBox(com.codename1.maps.BoundingBox) ArrayList(java.util.ArrayList) Point(com.codename1.ui.geom.Point)

Example 23 with Coord

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

the class MapContainer method getScreenCoordinate.

/**
 * Returns the screen position for the coordinate in component relative position
 * @param lat the latitude
 * @param lon the longitude
 * @return the x/y position in component relative position
 */
public Point getScreenCoordinate(double lat, double lon) {
    if (internalNative == null) {
        if (internalLightweightCmp != null) {
            Point p = internalLightweightCmp.getPointFromCoord(new Coord(lat, lon));
            p.setX(p.getX());
            p.setY(p.getY());
            return p;
        }
        Point p = dummyMapComponent.getPointFromCoord(new Coord(lat, lon));
        p.setX(p.getX());
        p.setY(p.getY());
        return p;
    // browserBridge.waitForReady();
    // String coord = (String)browserBridge.bridge.call("getScreenCoord", new Object[]{lat, lon});
    /*
            String coord = (String)internalBrowser.executeAndWait("callback.onSuccess("+BRIDGE+".getScreenCoord(${0}, ${1}))", new Object[]{lat, lon}).toString();
            try {
                String xStr = coord.substring(0, coord.indexOf(" "));
                String yStr = coord.substring(coord.indexOf(" ")+1);
                Point out =  new Point(
                        (int)Double.parseDouble(xStr), 
                        (int)Double.parseDouble(yStr)
                );
                //out.setX(out.getX() + getAbsoluteX());
                //out.setY(out.getY() + getAbsoluteY());
                return out;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            
            return new Point(0, 0);
            */
    }
    internalNative.calcScreenPosition(lat, lon);
    return new Point(internalNative.getScreenX() + getStyle().getPaddingLeft(false), internalNative.getScreenY() + getStyle().getPaddingTop());
}
Also used : Coord(com.codename1.maps.Coord) Point(com.codename1.ui.geom.Point)

Example 24 with Coord

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

the class MapLayout method mapPositionUpdated.

@Override
public void mapPositionUpdated(Component source, int zoom, Coord center) {
    // if (true) return;
    Runnable r = new Runnable() {

        public void run() {
            inUpdate = true;
            try {
                List<Coord> coords = new ArrayList<>();
                List<Component> cmps = new ArrayList<>();
                int len = actual.getComponentCount();
                for (Component current : actual) {
                    Coord crd = (Coord) current.getClientProperty(COORD_KEY);
                    coords.add(crd);
                    cmps.add(current);
                }
                int startingUpdateCounter = ++updateCounter;
                List<Point> points = map.getScreenCoordinates(coords);
                if (startingUpdateCounter != updateCounter || len != points.size()) {
                    // in which case, that update would be more recent than this one.
                    return;
                }
                for (int i = 0; i < len; i++) {
                    Component current = cmps.get(i);
                    Point p = points.get(i);
                    current.putClientProperty(POINT_KEY, p);
                }
                if (!pressed) {
                    // If the pointer is pressed
                    // we're showing the bitmap versions of the markers anyways
                    // so we don't need to revalidate the overlay aggressively.
                    actual.setShouldCalcPreferredSize(true);
                    actual.revalidate();
                }
                if (nextUpdate != null) {
                    Runnable nex = nextUpdate;
                    nextUpdate = null;
                    callSerially(nex);
                }
            } finally {
                inUpdate = false;
            }
        }
    };
    if (inUpdate) {
        nextUpdate = r;
    } else {
        nextUpdate = null;
        callSerially(r);
    }
}
Also used : Coord(com.codename1.maps.Coord) ArrayList(java.util.ArrayList) Point(com.codename1.ui.geom.Point) Component(com.codename1.ui.Component) Point(com.codename1.ui.geom.Point)

Example 25 with Coord

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

the class MapLayout method installMarkerFor.

private void installMarkerFor(Component c) {
    MapObject marker = (MapObject) c.getClientProperty(MARKER_KEY);
    if (marker != null) {
        map.removeMapObject(marker);
        markers.remove(marker);
        c.putClientProperty(MARKER_KEY, null);
    }
    Image im = generateMarkerImage(c);
    if (im == null) {
        return;
    }
    EncodedImage img = EncodedImage.createFromImage(generateMarkerImage(c), false);
    Float h = (Float) c.getClientProperty(HORIZONTAL_ALIGNMENT);
    if (h == null) {
        h = 0f;
    }
    Float v = (Float) c.getClientProperty(VERTICAL_ALIGNMENT);
    if (v == null) {
        v = 0f;
    }
    MarkerOptions markerOpts = new MarkerOptions((Coord) c.getClientProperty(COORD_KEY), img).anchorU(h).anchorV(v);
    marker = map.addMarker(markerOpts);
    c.putClientProperty(MARKER_KEY, marker);
    markers.add(marker);
}
Also used : MarkerOptions(com.codename1.googlemaps.MapContainer.MarkerOptions) Image(com.codename1.ui.Image) EncodedImage(com.codename1.ui.EncodedImage) MapObject(com.codename1.googlemaps.MapContainer.MapObject) EncodedImage(com.codename1.ui.EncodedImage)

Aggregations

Coord (com.codename1.maps.Coord)19 Point (com.codename1.ui.geom.Point)14 BoundingBox (com.codename1.maps.BoundingBox)7 Component (com.codename1.ui.Component)3 EncodedImage (com.codename1.ui.EncodedImage)3 ActionEvent (com.codename1.ui.events.ActionEvent)3 ActionListener (com.codename1.ui.events.ActionListener)3 Dimension (com.codename1.ui.geom.Dimension)3 MapObject (com.codename1.googlemaps.MapContainer.MapObject)2 Mercator (com.codename1.maps.Mercator)2 PointsLayer (com.codename1.maps.layers.PointsLayer)2 Button (com.codename1.ui.Button)2 Image (com.codename1.ui.Image)2 ArrayList (java.util.ArrayList)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 MarkerOptions (com.codename1.googlemaps.MapContainer.MarkerOptions)1 Util (com.codename1.io.Util)1