0

我正在使用适用于 Android 的 Google Maps API V2,我想知道 Google 的新导航抽屉是否可以与 MapFragement 交互。在抽屉里会有一个航点列表,当用户点击它时,地图将以点击的航点为中心。

对不起英语,谢谢。

4

1 回答 1

0

当然有可能。对于我的一个项目,我不使用它,但这里是方法。我使用了 SherlockMapFragment(为了更好的兼容性),但它与 MapFragment 相同

class DrawerItemClickListener implements OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
         case 0:
            SherlockMapFragment someFragment= new FragmentSome();
            Bundle args1 = new Bundle();
            args1.putInt("index", DATA_SLEEP); // here put some data
            someFragment.setArguments(args1);
            fm.beginTransaction().replace(R.id.contentFrame, someFragment)
                    .commit();
            getSupportActionBar().setTitle("I am fragment");
            drawerLayout.closeDrawer(drawerList);
            break;

然后在片段上创建捕获由地图片段实例化的新地图,将其存储在视图中,将其添加到膨胀布局并使用 gmap = getMap() 获取对它的引用,然后使用 gmap 这是地图对象:

    public class FragmentSome extends SherlockMapFragment{
private GoogleMap gMap;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        private View mapView;
        mapView = super.onCreateView(inflater, container, savedInstanceState); //

sore map in a view 
            initType = getArguments().getInt("index");
            loadData();
            return inflater.inflate(R.layout.fragment_food_sleep, container, false);
        }

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FrameLayout mapHolder = (FrameLayout) getView().findViewById(
                R.id.mapContainer);
        mapHolder.addView(mapView); // add map to inflated layout

            gMap = getMap(); // get reference to map and from here do whatever...

        if (initType == MainActivity.DATA_FOOD) {
            gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
                    46.390752, 16.437006), 5));
            gMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
        } else {
            gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
                    46.388322, 16.427994), 5));
            gMap.animateCamera(CameraUpdateFactory.zoomTo(13), 2000, null);
        }


        }
于 2013-07-18T20:50:12.083 回答