0

我正在为 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"/>
4

2 回答 2

1

If you are going to use Maps V1 with ActionBarSherlock, you need the add-on JAR for maps support -- basically a MapActivity that knows about ActionBarSherlock.

If you are going to use Maps V2 with ActionBarSherlock, you would typically load the map via a fragment, such as in this sample app. Nowhere does your sample code show how you are getting the map in there, and as such it is difficult to guess why the map is not displaying for you.

于 2013-02-19T18:59:46.857 回答
1

You're not properly setting the content view for the SherlockFragmentActivity, nor are you setting up the map when it expects to be setup.

In your onCreate method, make sure you do something like this after you call super.onCreate().

setContentView(R.layout.activity_main);
map = setupMap();

即使您正在从某个地方下载信息(或您在该异步任务中所做的任何事情),您仍然需要设置内容视图以在 onCreate 中正确填充地图。

您还需要确保从 onCreate 调用 setupMap。您可以稍后添加标记。

自 API v2 发布之日起,我一直在将 SherlockFragmentActivity 与 Google Maps Android API v2 一起使用,并且我没有遇到任何问题,因此它必须是您创建视图的顺序。

于 2013-02-19T22:11:46.727 回答