Example usage for com.google.gwt.maps.client.overlay HasProjection fromLatLngToPoint

List of usage examples for com.google.gwt.maps.client.overlay HasProjection fromLatLngToPoint

Introduction

In this page you can find the example usage for com.google.gwt.maps.client.overlay HasProjection fromLatLngToPoint.

Prototype

HasPoint fromLatLngToPoint(HasLatLng latLng);

Source Link

Usage

From source file:com.mashery.examples.api.client.weatherbug.WeatherBugOverlayView.java

License:Open Source License

@Override
public void draw() {
    HasLatLngBounds bounds = getMap().getBounds();
    HasMapCanvasProjection ovrPrj = getProjection();
    HasPoint ovrNE = ovrPrj.fromLatLngToDivPixel(bounds.getNorthEast());
    HasPoint ovrSW = ovrPrj.fromLatLngToDivPixel(bounds.getSouthWest());

    div.getStyle().setLeft(ovrSW.getX(), Unit.PX);
    div.getStyle().setTop(ovrNE.getY(), Unit.PX);
    div.getStyle().setWidth(ovrNE.getX() - ovrSW.getX(), Unit.PX);
    div.getStyle().setHeight(ovrSW.getY() - ovrNE.getY(), Unit.PX);

    String endpoint = WEATHERBUG_ENDPOINT.replace("${layerType}", layerType.getPath());
    HashSet<TileKey> usedTiles = new HashSet<TileKey>();

    int zoom = getMap().getZoom();
    double scale = Math.pow(2d, zoom);
    HasProjection prj = getMap().getProjection();
    LatLng nwLatLng = new LatLng(bounds.getNorthEast().getLatitude(), bounds.getSouthWest().getLongitude());
    HasPoint nwRaw = prj.fromLatLngToPoint(nwLatLng);
    LatLng seLatLng = new LatLng(bounds.getSouthWest().getLatitude(), bounds.getNorthEast().getLongitude());
    HasPoint seRaw = prj.fromLatLngToPoint(seLatLng);

    Point nw = new Point(Math.floor(nwRaw.getX() * scale), Math.floor(nwRaw.getY() * scale));
    Point se = new Point(Math.floor(seRaw.getX() * scale), Math.floor(seRaw.getY() * scale));

    double offsetX = nw.getX() % 256d;
    double offsetY = nw.getY() % 256d;
    Point startPoint = new Point(nw.getX() - offsetX, nw.getY() - offsetY);

    while (startPoint.getY() < se.getY()) {
        Point p = new Point(startPoint.getX(), startPoint.getY());
        startPoint = new Point(startPoint.getX(), startPoint.getY() + 256);
        while (p.getX() < se.getX()) {
            int tx = (int) p.getX() / 256;
            int ty = (int) p.getY() / 256;

            TileKey tileKey = new TileKey(tx, ty, zoom);
            usedTiles.add(tileKey);/*from w w  w. j  a  va 2s.co  m*/

            ImageElement img = tiles.remove(tileKey);
            if (img == null) {
                img = Document.get().createImageElement();
                img.getStyle().setBorderStyle(BorderStyle.NONE);
                img.getStyle().setBorderWidth(0d, Unit.PX);
                img.getStyle().setPosition(Position.ABSOLUTE);

                StringBuilder buf = new StringBuilder(endpoint);
                buf.append("&tx=").append(tx);
                buf.append("&ty=").append(ty);
                buf.append("&zm=").append(zoom);
                img.setSrc(buf.toString());

                div.appendChild(img);
            } else {
                img.getStyle().setVisibility(Visibility.VISIBLE);
            }

            img.getStyle().setLeft(p.getX() - nw.getX(), Unit.PX);
            img.getStyle().setTop(p.getY() - nw.getY(), Unit.PX);
            img.getStyle().setWidth(256d, Unit.PX);
            img.getStyle().setHeight(256d, Unit.PX);
            tiles.put(tileKey, img);

            p = new Point(p.getX() + 256, p.getY());
        }
    }

    for (Iterator<Map.Entry<TileKey, ImageElement>> i = tiles.entrySet().iterator(); i.hasNext();) {
        Map.Entry<TileKey, ImageElement> entry = i.next();
        if (!usedTiles.remove(entry.getKey())) {
            if (tiles.size() > TILE_CACHE_SIZE) {
                i.remove();
                entry.getValue().removeFromParent();
            } else {
                entry.getValue().getStyle().setVisibility(Visibility.HIDDEN);
            }
        }
    }
}