Exemple #1
0
 /** Sets the displayed map pane and zoom level so that all map markers are visible. */
 public void setDisplayToFitMapMarkers() {
   if (mapMarkerList == null || mapMarkerList.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 (MapMarker marker : mapMarkerList) {
     int x = OsmMercator.LonToX(marker.getLon(), mapZoomMax);
     int y = OsmMercator.LatToY(marker.getLat(), mapZoomMax);
     x_max = Math.max(x_max, x);
     y_max = Math.max(y_max, y);
     x_min = Math.min(x_min, x);
     y_min = Math.min(y_min, y);
   }
   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);
 }
Exemple #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);
        }
      }
    }
  }