3

我的问题很简单,我想在 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);
    }
}

瓷砖提供者如下所示:

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));
}

但是我可以我的图块永远不会加载到地图中。其他谷歌地图瓷砖工作正常......

有什么建议么?

4

2 回答 2

1

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

于 2015-04-22T17:57:14.667 回答
0

您可以保留对 TileProvider 的引用,然后在需要时调用 .remove() 将其从地图中删除:

TileOverlay mTileOverlay;

像以前一样添加瓷砖:

TileProvider tileProvider = new OSMTileProvider();
mTileOverlay = mMap.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider));

像这样删除它:

if (null != mTileOverlay) {
    mTileOverlay.remove();
}
于 2016-06-17T19:18:10.183 回答