use of com.codename1.maps.Coord 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);
*/
}
use of com.codename1.maps.Coord 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;
}
}
}
use of com.codename1.maps.Coord in project codenameone-google-maps by codenameone.
the class MapContainer method addPath.
/**
* Draws a path on the map
* @param path the path to draw on the map
* @return a map object instance that allows us to remove the drawn path
*/
public MapObject addPath(Coord... path) {
if (internalNative != null) {
long key = internalNative.beginPath();
for (Coord c : path) {
internalNative.addToPath(key, c.getLatitude(), c.getLongitude());
}
key = internalNative.finishPath(key);
MapObject o = new MapObject();
o.mapKey = key;
markers.add(o);
return o;
} else {
if (internalLightweightCmp != null) {
LinesLayer ll = new LinesLayer();
ll.addLineSegment(path);
internalLightweightCmp.addLayer(ll);
MapObject o = new MapObject();
o.lines = ll;
markers.add(o);
return o;
} else {
// browserBridge.waitForReady();
StringBuilder json = new StringBuilder();
json.append("[");
boolean first = true;
for (Coord c : path) {
if (first) {
first = false;
} else {
json.append(", ");
}
json.append("{\"lat\":").append(c.getLatitude()).append(", \"lon\": ").append(c.getLongitude()).append("}");
}
json.append("]");
// long key = ((Double)browserBridge.bridge.call("addPathAsJSON", new Object[]{json.toString()})).intValue();
MapObject o = new MapObject();
o.pending = true;
browserBridge.ready(() -> {
internalBrowser.execute("callback.onSuccess(" + BRIDGE + ".addPathAsJSON(${0}));", new Object[] { json.toString() }, jsres -> {
o.mapKey = jsres.getInt();
o.pending = false;
});
});
markers.add(o);
return o;
}
}
}
use of com.codename1.maps.Coord in project codenameone-google-maps by codenameone.
the class MapLayout method layoutContainer.
@Override
public void layoutContainer(Container parent) {
// if (true) {
// return;
// }
int parentX = 0;
int parentY = 0;
for (Component current : parent) {
Coord crd = (Coord) current.getClientProperty(COORD_KEY);
Point p = (Point) current.getClientProperty(POINT_KEY);
if (p == null) {
p = map.getScreenCoordinate(crd);
current.putClientProperty(POINT_KEY, p);
}
Float h = (Float) current.getClientProperty(HORIZONTAL_ALIGNMENT);
if (h == null) {
h = 0f;
}
Float v = (Float) current.getClientProperty(VERTICAL_ALIGNMENT);
if (v == null) {
v = 0f;
}
current.setSize(current.getPreferredSize());
current.setX(convertX(p.getX() - parentX, current.getWidth(), h));
current.setY(convertY(p.getY() - parentY, current.getHeight(), v));
}
}
use of com.codename1.maps.Coord in project codenameone-google-maps by codenameone.
the class MapInfoPanel method createMarkerButton.
private Button createMarkerButton(MapObject mo, String label, Image icon, final Coord location) {
Button b = new Button(label, icon);
b.addActionListener(e -> {
map.zoom(location, (int) map.getZoom());
});
return b;
}
Aggregations