我的问题很简单,我想在 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));
}
但是我可以我的图块永远不会加载到地图中。其他谷歌地图瓷砖工作正常......
有什么建议么?