moocdev / osmdroid

Automatically exported from code.google.com/p/osmdroid
0 stars 0 forks source link

Only the nine center tiles are shown with GoogleTilesOverlay (osmdroid-third-party) #326

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Use device with a big screen/resolution (like Galaxy Nexus phone or Asus 
Transformer tablet).
2. Use the GoogleTilesOverlay class from the osmdroid-third-party jar to shown 
an OSM layer on top of a base google map layer.

What is the expected output? What do you see instead?
I expect the OSM tiles to be loaded for the complete screen/view. Instead only 
the nine center OSM tiles are shown on top of the complete google map layer. 
The OSM tiles along the edges of the phone/tablet are not shown.

What version of the product are you using? On what operating system?
osmdroid-third-party-3.0.7.jar

Please provide any additional information below.
I created a solution which I will attach to this issue.

Original issue reported on code.google.com by alex.vanderlinden on 25 Mar 2012 at 6:39

GoogleCodeExporter commented 8 years ago
I updated the draw() method at org.osmdroid.google.overlay.GoogleTilesOverlay 
at my local environment to below code. This works for me. I think this fixes 
the issue.

==============
    @Override
    public void draw(final Canvas c, final MapView osmv, final boolean shadow) {
        if (DEBUGMODE) {
            logger.trace("draw");
        }

        // Calculate the half-world size
        final Projection pj = osmv.getProjection();
        final int zoomLevel = osmv.getZoomLevel() - 1;
        final int tileSizePx = this.mTileProvider.getTileSource().getTileSizePixels();

        // Calculate the tiles needed for each side around the center one.
        final int latSpan = osmv.getLatitudeSpan();
        final int longSpan = osmv.getLongitudeSpan();
        final int topLatitude = osmv.getMapCenter().getLatitudeE6() + latSpan/2;
        final int leftLongitude = osmv.getMapCenter().getLongitudeE6() - longSpan/2;
        final int bottomLatitude = osmv.getMapCenter().getLatitudeE6() - latSpan/2;
        final int rightLongitude = osmv.getMapCenter().getLongitudeE6() + longSpan/2;
        final Point leftTopXY = Mercator.projectGeoPoint(topLatitude/1E6, leftLongitude/1E6, zoomLevel, new Point(0,0));
        final Point rightBottomXY = Mercator.projectGeoPoint(bottomLatitude/1E6, rightLongitude/1E6, zoomLevel, new Point(0,0));
        final int tileNeededAtLeft = leftTopXY.x;
        final int tileNeededAtRight = rightBottomXY.x;
        final int tileNeededAtTop = leftTopXY.y;
        final int tileNeededAtBottom = rightBottomXY.y;

        final int mapTileUpperBound = 1 << zoomLevel;
        // make sure the cache is big enough for all the tiles
        final int numNeeded = (tileNeededAtBottom - tileNeededAtTop + 1)
                * (tileNeededAtRight - tileNeededAtLeft + 1);
        mTileProvider.ensureCapacity(numNeeded);
        /* Draw all the MapTiles (from the upper left to the lower right). */
        for (int y = tileNeededAtTop; y <= tileNeededAtBottom; y++) {
            for (int x = tileNeededAtLeft; x <= tileNeededAtRight; x++) {
                // Construct a MapTile to request from the tile provider.
                final int tileY = MyMath.mod(y, mapTileUpperBound);
                final int tileX = MyMath.mod(x, mapTileUpperBound);
                final MapTile tile = new MapTile(zoomLevel, tileX, tileY);
                final Drawable currentMapTile = mTileProvider.getMapTile(tile);
                if (currentMapTile != null) {
                    final GeoPoint gp = new GeoPoint(
                            (int) (Mercator.tile2lat(y, zoomLevel) * 1E6),
                            (int) (Mercator.tile2lon(x, zoomLevel) * 1E6));
                    pj.toPixels(gp, mTilePos);
                    mTileRect.set(mTilePos.x, mTilePos.y, mTilePos.x + tileSizePx, mTilePos.y + tileSizePx);
                    currentMapTile.setBounds(mTileRect);
                    currentMapTile.draw(c);
                }

                if (DEBUGMODE) {
                    c.drawText(tile.toString(), mTileRect.left + 1, mTileRect.top + mPaint.getTextSize(), mPaint);
                    c.drawLine(mTileRect.left, mTileRect.top, mTileRect.right, mTileRect.top, mPaint);
                    c.drawLine(mTileRect.left, mTileRect.top, mTileRect.left, mTileRect.bottom, mPaint);
                }
            }
        }

        // draw a cross at center in debug mode
        if (DEBUGMODE) {
            final GeoPoint center = osmv.getMapCenter();
            final Point centerPoint = pj.toPixels(center, null);
            c.drawLine(centerPoint.x, centerPoint.y - 9, centerPoint.x, centerPoint.y + 9, mPaint);
            c.drawLine(centerPoint.x - 9, centerPoint.y, centerPoint.x + 9, centerPoint.y, mPaint);
        }
    }
==============

Original comment by alex.vanderlinden on 25 Mar 2012 at 6:45

GoogleCodeExporter commented 8 years ago
I haven't tested it, but I checked this in in revision 1082. It seems to make 
sense ;-)

Original comment by neilboyd on 26 Mar 2012 at 7:54

GoogleCodeExporter commented 8 years ago

Original comment by neilboyd on 9 Apr 2012 at 2:41