Android Drawer示例,如何实现Google Map Fragment

时间:2014-04-14 20:10:36

标签: android google-maps android-fragments google-maps-android-api-2

其实我正在尝试在Android Drawer示例上实现Google Map,基本上我想通过选择第一个菜单项来显示地图。所有提供的示例都是对所有地图使用FragmentActivity,我安排扩展Fragment类(由抽屉使用),创建一个适合Map的类。不幸的是App在发布时崩溃了:

主要活动

public class HLMainActivity extends ActionBarActivity implements
    NavigationDrawerFragment.NavigationDrawerCallbacks {

/**
 * Fragment managing the behaviors, interactions and presentation of the
 * navigation drawer.
 */
private NavigationDrawerFragment mNavigationDrawerFragment;

/**
 * Used to store the last screen title. For use in
 * {@link #restoreActionBar()}.
 */
private CharSequence mTitle;

public static FragmentManager fragmentManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hlmain);

    // initialising the object of the FragmentManager. Here I'm passing getSupportFragmentManager(). You can pass getFragmentManager() if you are coding for Android 3.0 or above.
    fragmentManager = getSupportFragmentManager();

    mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
            .findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
}

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    fragmentManager = getSupportFragmentManager();

    HLMainMapFragment fragment = new HLMainMapFragment();
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, fragment).commit();

}

public void onSectionAttached(int number) {
    switch (number) {
    case 1:
        mTitle = getString(R.string.title_section1);
        break;
    case 2:
        mTitle = getString(R.string.title_section2);
        break;
    case 3:
        mTitle = getString(R.string.title_section3);
        break;
    }
}

public void restoreActionBar() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        // Only show items in the action bar relevant to this screen
        // if the drawer is not showing. Otherwise, let the drawer
        // decide what to show in the action bar.
        getMenuInflater().inflate(R.menu.hlmain, menu);
        restoreActionBar();
        return true;
    }
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}   

}

地图片段

public class HLMainMapFragment extends Fragment {
private static View view;
/**
 * Note that this may be null if the Google Play services APK is not
 * available.
 */

private static GoogleMap mMap;
private static Double latitude, longitude;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (container == null) {
        return null;
    }

    view = (RelativeLayout) inflater.inflate(R.layout.location_fragment,
            container, false);
    // Passing harcoded values for latitude & longitude. Please change as
    // per your need. This is just used to drop a Marker on the Map
    latitude = 26.78;
    longitude = 72.56;

    setUpMapIfNeeded(); // For setting up the MapFragment

    return view;
}

/***** Sets up the map if it is possible to do so *****/
public static 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) HLMainActivity.fragmentManager
                .findFragmentById(R.id.map)).getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null)
            setUpMap();
    }
}

/**
 * This is where we can add markers or lines, add listeners or move the
 * camera.
 * <p>
 * This should only be called once and when we are sure that {@link #mMap}
 * is not null.
 */
private static void setUpMap() {
    // For showing a move to my loction button
    mMap.setMyLocationEnabled(true);
    // For dropping a marker at a point on the Map
    mMap.addMarker(new MarkerOptions()
            .position(new LatLng(latitude, longitude)).title("My Home")
            .snippet("Home Address"));
    // For zooming automatically to the Dropped PIN Location
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
            latitude, longitude), 12.0f));
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    if (mMap != null)
        setUpMap();

    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) HLMainActivity.fragmentManager
                .findFragmentById(R.id.map)).getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null)
            setUpMap();
    }
}

/****
 * The mapfragment's id must be removed from the FragmentManager or else if
 * the same it is passed on the next time then app will crash
 ****/
@Override
public void onDestroyView() {
    super.onDestroyView();
    if (mMap != null) {
        HLMainActivity.fragmentManager
                .beginTransaction()
                .remove(HLMainActivity.fragmentManager
                        .findFragmentById(R.id.map)).commit();
        mMap = null;
    }
}
}

位置片段XML

<?xml version="1.0" encoding="utf-8"?>
<MapFragment 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.MapFragment" />

logcat的

    04-14 16:43:36.984: E/AndroidRuntime(1159): FATAL EXCEPTION: main
    04-14 16:43:36.984: E/AndroidRuntime(1159): Process: com.studiohangloose.iMobility, PID: 1159
    04-14 16:43:36.984: E/AndroidRuntime(1159): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.studiohangloose.iMobility/com.studiohangloose.iMobility.HLMainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class MapFragment
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.os.Handler.dispatchMessage(Handler.java:102)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.os.Looper.loop(Looper.java:136)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.app.ActivityThread.main(ActivityThread.java:5017)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at java.lang.reflect.Method.invokeNative(Native Method)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at java.lang.reflect.Method.invoke(Method.java:515)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at dalvik.system.NativeStart.main(Native Method)
    04-14 16:43:36.984: E/AndroidRuntime(1159): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class MapFragment
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at com.studiohangloose.iMobility.HLMainMapFragment.onCreateView(HLMainMapFragment.java:33)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.app.Activity.performStart(Activity.java:5241)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     ... 11 more
    04-14 16:43:36.984: E/AndroidRuntime(1159): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.MapFragment" on path: DexPathList[[zip file "/data/app/com.studiohangloose.iMobility-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.studiohangloose.iMobility-2, /system/lib]]
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.view.LayoutInflater.createView(LayoutInflater.java:559)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:652)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
    04-14 16:43:36.984: E/AndroidRuntime(1159):     ... 23 more

有任何建议!?或者也许是解决方案的链接......

2 个答案:

答案 0 :(得分:10)

如果您想在fragment课程中使用地图,请使用MapView

MapView mapView;

GoogleMap googleMap;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        View view = inflater.inflate(R.layout.your_layout_name, container, false);

        mapView = (MapView) view.findViewById(R.id.map);

            mapView.onCreate(savedInstanceState);

            if(mapView!=null)
            {
                googleMap = mapView.getMap();

                googleMap.getUiSettings().setMyLocationButtonEnabled(false);

                googleMap.setMyLocationEnabled(true);

                googleMap.getUiSettings().setZoomControlsEnabled(true);
            }

        return view;

    }

  @Override
    public void onResume() 
    {
        mapView.onResume();

        super.onResume();
    }
    @Override
    public void onDestroy() 
    {
        super.onDestroy();

        mapView.onDestroy();
    }
    @Override
    public void onLowMemory() 
    {
        super.onLowMemory();

        mapView.onLowMemory();
    }

xml文件粘贴代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

试试这个你会看到一个好结果......

答案 1 :(得分:0)

更新位置片段XML ,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.MapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

在您的片段中实现OnMapReadyCallback界面,并编写用于在onMapReady(GoogleMap map)方法中配置地图的代码,例如:

public void onMapReady(GoogleMap map) {
    googleMap.addMarker(new MarkerOptions()
             .position(new LatLng(0, 0))
             .title("Marker"));

    googleMap.setMyLocationEnabled(true);
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(17.0f) );
}

我希望它会对你有所帮助!