Android Google API V2 启用/禁用 TileProvider

标签 android google-maps android-maps-v2

我的问题很简单,我想在 Google Maps API v2 上启用和禁用 TileUrlProvider。

我的做法是:

public class BackTileSelectorActivity extends FragmentActivity {

    private GoogleMap mMap;
    private TileOverlayOptions osmTileProviderOptions;
    private TileOverlayOptions moonTileProviderOptions;

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

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

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        mMap.animateCamera(
                CameraUpdateFactory.newLatLngZoom(
                        new LatLng(47.224217, -1.631409), 
                        16
                    )
                );

        osmTileProviderOptions = new TileOverlayOptions().tileProvider(new OSMTileProvider());
        osmTileProviderOptions.visible(false);
        mMap.addTileOverlay(osmTileProviderOptions);

        moonTileProviderOptions = new TileOverlayOptions().tileProvider(new MoonTileProvider());
        moonTileProviderOptions.visible(false);
        mMap.addTileOverlay(moonTileProviderOptions);
    }

    public void setGoogleMapNormalEnabled(View v) {
        osmTileProviderOptions.visible(false);
        moonTileProviderOptions.visible(false);
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    }

    public void setOSMMapEnabled(View v) {
        osmTileProviderOptions.visible(true);
        moonTileProviderOptions.visible(false);
        mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
    }

    public void setMoonMapEnabled(View v) {
        osmTileProviderOptions.visible(false);
        moonTileProviderOptions.visible(true);
        mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
    }

    public void setGoogleMapHybridEnabled(View v) {
        osmTileProviderOptions.visible(false);
        moonTileProviderOptions.visible(false);
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    }

    public void setGoogleMapSatelliteEnabled(View v) {
        osmTileProviderOptions.visible(false);
        moonTileProviderOptions.visible(false);
        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    }

    public void setGoogleMapTerrainEnabled(View v) {
        osmTileProviderOptions.visible(false);
        moonTileProviderOptions.visible(false);
        mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
    }
}

tile 提供者看起来像这样:

public class MoonTileProvider extends UrlTileProvider {

    private static final int TILE_WIDTH = 256;
    private static final int TILE_HEIGHT = 256;

     private static final String MOON_MAP_URL_FORMAT =
                "http://mw1.google.com/mw-planetary/lunar/lunarmaps_v1/clem_bw/%d/%d/%d.jpg";

    public MoonTileProvider() {
        super(TILE_WIDTH, TILE_HEIGHT);
    }

    @Override
    public URL getTileUrl(int x, int y, int zoom) {
        // The moon tile coordinate system is reversed.  This is not normal.
        int reversedY = (1 << zoom) - y - 1;
        String s = String.format(Locale.US, MOON_MAP_URL_FORMAT, zoom, x, reversedY);
        URL url = null;
        try {
            url = new URL(s);
        } catch (MalformedURLException e) {
            throw new AssertionError(e);
        }
        return url;
    }

}

和:

public class OSMTileProvider extends UrlTileProvider {

    private static final int TILE_WIDTH = 256;
    private static final int TILE_HEIGHT = 256;

    private static final String OSM_MAP_URL_FORMAT = "http://a.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/997/256/%d/%d/%d.png";

    public OSMTileProvider() {
        super(TILE_WIDTH, TILE_HEIGHT);
    }

    @Override
    public URL getTileUrl(int x, int y, int zoom) {
        String s = String.format(Locale.US, OSM_MAP_LOCAL_URL_FORMAT, zoom, x, y);

        URL url = null;
        try {
            url = new URL(s);
        } catch (MalformedURLException e) {
            return null;
        }
        return url;
    }

}

像这样调用时,tileProvider 工作正常:

private GoogleMap mMap;

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

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

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {
    mMap.setMapType(GoogleMap.MAP_TYPE_NONE);

    mMap.animateCamera(
            CameraUpdateFactory.newLatLngZoom(
                    new LatLng(47.224217, -1.631409), 
                    16
                )
            );

    TileProvider tileProvider = new OSMTileProvider();

    mMap.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider));
}

但我可以,我的图 block 从未加载到 map 中。 其他谷歌地图图 block 工作正常......

有什么建议吗?

最佳答案

我遇到了同样的问题。事实证明,.visible() 函数除了返回对象外没有做任何事情,但可见性发生了变化。 因此,如果您必须更改可见性,我建议您在 map 对象上执行 .clear(),然后重新添加叠加层。如果有其他解决方案,请告诉我。

关于Android Google API V2 启用/禁用 TileProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14253790/

相关文章:

java - .dex 文件中引用的方法数量不能超过 64K 和 java.lang.UnsupportedOperationException

java - Recyclerview android studio "hiding"?

android - Jetpack Compose TextField InputFilter 仅具有货币正则表达式输入

java - 如何在不牺牲性能的情况下创建 Google Plus 应用程序的布局?

android - 使用原生 Google map 的 PhoneGap 方向

javascript - Google Maps API 没有将正确的位置居中

google-maps - 谷歌地球或谷歌地图测地线上的直线是直线吗?

android - 打开街道 map 离线工作 android

android - 需要在用户位置周围使用不同颜色的边框圆圈