Пример #1
0
 /** Sets the displayed map pane and zoom level so that all map markers are visible. */
 public void setDisplayToFitMapRectangle() {
   if (mapRectangleList == null || mapRectangleList.size() == 0) {
     return;
   }
   int x_min = Integer.MAX_VALUE;
   int y_min = Integer.MAX_VALUE;
   int x_max = Integer.MIN_VALUE;
   int y_max = Integer.MIN_VALUE;
   int mapZoomMax = tileController.getTileSource().getMaxZoom();
   for (MapRectangle rectangle : mapRectangleList) {
     x_max = Math.max(x_max, OsmMercator.LonToX(rectangle.getBottomRight().getLon(), mapZoomMax));
     y_max = Math.max(y_max, OsmMercator.LatToY(rectangle.getTopLeft().getLat(), mapZoomMax));
     x_min = Math.min(x_min, OsmMercator.LonToX(rectangle.getTopLeft().getLon(), mapZoomMax));
     y_min = Math.min(y_min, OsmMercator.LatToY(rectangle.getBottomRight().getLat(), mapZoomMax));
   }
   int height = Math.max(0, getHeight());
   int width = Math.max(0, getWidth());
   // System.out.println(x_min + " < x < " + x_max);
   // System.out.println(y_min + " < y < " + y_max);
   // System.out.println("tiles: " + width + " " + height);
   int newZoom = mapZoomMax;
   int x = x_max - x_min;
   int y = y_max - y_min;
   while (x > width || y > height) {
     // System.out.println("zoom: " + zoom + " -> " + x + " " + y);
     newZoom--;
     x >>= 1;
     y >>= 1;
   }
   x = x_min + (x_max - x_min) / 2;
   y = y_min + (y_max - y_min) / 2;
   int z = 1 << (mapZoomMax - newZoom);
   x /= z;
   y /= z;
   setDisplayPosition(x, y, newZoom);
 }
Пример #2
0
  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    int iMove = 0;

    int tilex = center.x / Tile.SIZE;
    int tiley = center.y / Tile.SIZE;
    int off_x = (center.x % Tile.SIZE);
    int off_y = (center.y % Tile.SIZE);

    int w2 = getWidth() / 2;
    int h2 = getHeight() / 2;
    int posx = w2 - off_x;
    int posy = h2 - off_y;

    int diff_left = off_x;
    int diff_right = Tile.SIZE - off_x;
    int diff_top = off_y;
    int diff_bottom = Tile.SIZE - off_y;

    boolean start_left = diff_left < diff_right;
    boolean start_top = diff_top < diff_bottom;

    if (start_top) {
      if (start_left) iMove = 2;
      else iMove = 3;
    } else {
      if (start_left) iMove = 1;
      else iMove = 0;
    } // calculate the visibility borders
    int x_min = -Tile.SIZE;
    int y_min = -Tile.SIZE;
    int x_max = getWidth();
    int y_max = getHeight();

    // paint the tiles in a spiral, starting from center of the map
    boolean painted = true;
    int x = 0;
    while (painted) {
      painted = false;
      for (int i = 0; i < 4; i++) {
        if (i % 2 == 0) x++;
        for (int j = 0; j < x; j++) {
          if (x_min <= posx && posx <= x_max && y_min <= posy && posy <= y_max) {
            // tile is visible
            Tile tile = tileController.getTile(tilex, tiley, zoom);
            if (tile != null) {
              painted = true;
              tile.paint(g, posx, posy);
              if (tileGridVisible) g.drawRect(posx, posy, Tile.SIZE, Tile.SIZE);
            }
          }
          Point p = move[iMove];
          posx += p.x * Tile.SIZE;
          posy += p.y * Tile.SIZE;
          tilex += p.x;
          tiley += p.y;
        }
        iMove = (iMove + 1) % move.length;
      }
    }

    for (OverlayPainter op : overlayPainterList) {
      op.paintOverlay(g);
    }
    // outer border of the map
    int mapSize = Tile.SIZE << zoom;
    g.drawRect(w2 - center.x, h2 - center.y, mapSize, mapSize);

    // g.drawString("Tiles in cache: " + tileCache.getTileCount(), 50, 20);

    if (mapRectanglesVisible && mapRectangleList != null) {
      for (MapRectangle rectangle : mapRectangleList) {
        Coordinate topLeft = rectangle.getTopLeft();
        Coordinate bottomRight = rectangle.getBottomRight();
        if (topLeft != null && bottomRight != null) {
          Point pTopLeft = getMapPosition(topLeft.getLat(), topLeft.getLon(), false);
          Point pBottomRight = getMapPosition(bottomRight.getLat(), bottomRight.getLon(), false);
          if (pTopLeft != null && pBottomRight != null) {
            rectangle.paint(g, pTopLeft, pBottomRight);
          }
        }
      }
    }

    if (mapMarkersVisible && mapMarkerList != null) {
      for (MapMarker marker : mapMarkerList) {
        Point p = getMapPosition(marker.getLat(), marker.getLon());
        if (p != null) {
          marker.paint(g, p);
        }
      }
    }
  }