【发布时间】:2013-08-19 06:22:06
【问题描述】:
我正在使用 google maps android api 开发一个应用程序。该应用程序显示用户可以打开和关闭的各种图块叠加。当我旋转我的设备时,覆盖保持在原位,但是当我单击按钮关闭我的覆盖时,我的设备就像控制我的覆盖的变量为空一样。在这种情况下,我怎样才能保持对我的 tileoverlay 的控制。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
//mMap=null;
if (savedInstanceState == null) {
// First incarnation of this activity.
mapFragment.setRetainInstance(true);
} else {
// Reincarnated activity. The obtained map is the same map instance in the previous
// activity life cycle. There is no need to reinitialize it.
mMap = mapFragment.getMap();
}
setUpMapIfNeeded();
setUpFieldsIfNeeded();
}
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();
}
}
}
private void setUpFieldsIfNeeded(){
if (fieldsOverlay == null) {
TileProvider tileProvider = new UrlTileProvider(256, 256) {
@Override
public synchronized URL getTileUrl(int x, int y, int zoom) {
//String s = String.format(Locale.US, CMAFieldsRequestFormat, zoom, x, y);
String s = String.format(Locale.US, cartoDBFieldsTile, zoom, x, y);
s =s.replace(" ", "%20");
System.out.println(s);
URL url = null;
try {
url = new URL(s);
} catch (MalformedURLException e) {
throw new AssertionError(e);
}
return url;
}
};
fieldsOverlay = mMap.addTileOverlay(
new TileOverlayOptions().tileProvider(tileProvider));
fieldsOverlay.setVisible(false);
fieldsOverlay.setZIndex(1);}
}
TLDR:我正在使用带有名为 fieldsOverlay 的 tileOverlay 的谷歌地图。当我旋转我的设备时,覆盖层本身会被保留,但它不再连接到变量 fieldsOverlay。解决此问题的最佳方法是什么。
谢谢
编辑:通过运行 mMap.clear 然后重新初始化我的图层并将可见性设置为 savedInstanceState 值,我能够让应用程序看起来像我想要的那样运行,但这似乎不像一个很好的解决方案。
【问题讨论】:
标签: android google-maps-android-api-2