私の問題は単純です。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));
}
しかし、タイルがマップにロードされることはありません。他のGoogleマップのタイルは正常に動作します...
助言がありますか?