我正在为 android 开发一个 GoogleMaps v2 API 应用程序并且遇到了一个问题。我想使用一个 ActionBar(通过 actionbarsherlock,用于向后兼容),如下图所示。
(来源:google.com)
如果不调整我的代码以拥有 ActionBar,我的地图应用程序就可以很好地工作。我尝试使代码一致以使用它,但会导致空白屏幕和操作栏。因此,地图本身或其他任何东西都会被加载。
之前的代码:(仅相关部分)
public class MapActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initiate loader, so that files can be acquired in background thread.
new Loader().execute();
}
.
.
.
}
我没有包括导入或变量。不用担心,因为代码确实可以编译。
之后的代码:
public class MapActivity extends SherlockFragmentActivity {
public boolean onCreate(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0,SEARCH,0,"Search")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
// Initiate loader, so that files can be acquired in background thread.
new Loader().execute();
return true;
}
public boolean onOptionsItemSelected (MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case SEARCH:
openSearchView();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
.
.
.
}
编辑 -
地图设置代码:(注意map
已声明为类变量)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Recovers map choice from previous activity.
Intent intent = getIntent();
map_descriptor = intent.getStringExtra(MAP);
MAP_CENTER = map2LatLng(map_descriptor);
map = setupMap(map);
// Initiate loader, so that files can be acquired in background thread.
new Loader().execute();
}
public GoogleMap setupMap(GoogleMap map) {
if (map == null) {
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// Just in case null map was accidently passed to setupMap.
}
map.setMapType(3); // TERRAIN
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(MAP_CENTER)
.zoom(ZOOM_LEVEL)
.build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
map.setOnCameraChangeListener(new OnCameraChangeListener() {
public void onCameraChange(CameraPosition cameraPosition) {
ZOOM_LEVEL = cameraPosition.zoom;
recenterMap(ZOOM_LEVEL);
}
});
return map;
}
和 XML 布局代码:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>