自定义图块不会显示在Google地图上

时间:2014-10-02 13:11:48

标签: android google-maps google-maps-android-api-2 tile

我设置了一个自定义UrlTileProvider来在MapsActivity中呈现切片,如下所示:

public class MapsActivity extends FragmentActivity {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {
        if (mMap == null) {
            mMap = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        TileProvider tileProvider = new CustomTileProvider();
        TileOverlayOptions tileOverlayOptions = new TileOverlayOptions()
            .tileProvider(tileProvider);
        tileOverlayOptions.visible(true);
        tileOverlayOptions.zIndex(10000);
        TileOverlay tileOverlay = mMap.addTileOverlay(tileOverlayOptions);
    }
}

...

public class CustomTileProvider extends UrlTileProvider {

    private static final int TILE_WIDTH = 256;
    private static final int TILE_HEIGHT = 256;
    private static final int MIN_ZOOM = 12;
    private static final int MAX_ZOOM = 16;
    private static final String TILE_SERVER_URL = ...

    public CustomTileProvider() {
        this(TILE_WIDTH, TILE_HEIGHT);
    }

    public CustomTileProvider(final int width, final int height) {
        super(width, height);
    }

    @Override
    public URL getTileUrl(int x, int y, int zoom) {
        String tileServerUrl = String.format(TILE_SERVER_URL, zoom, x, y);

        if (!checkTileExists(x, y, zoom)) {
            return null;
        }

        try {
            return new URL(tileServerUrl);
        } catch (MalformedURLException exception) {
            throw new AssertionError(exception);
        }
    }

    private boolean checkTileExists(int x, int y, int zoom) {
        if ((zoom < MIN_ZOOM || zoom > MAX_ZOOM)) {
            return false;
        }
        return true;
    }

}

这基本上与Google documentation中的相同。

由于某些原因,服务器提供的区域没有显示任何图块。永远不会调用方法getTileUrl()

1 个答案:

答案 0 :(得分:0)

问题在于String.format会抛出IllegalFormatException,但这不会显示在日志中。

我建议用try ... catch块包围它。

此外,%中的网址编码TILE_SERVER_URL会干扰String.format(TILE_SERVER_URL, zoom, x, y)中的格式占位符。