2013-07-23 18 views
9

Tôi đang cố truy xuất đoạn Bản đồ của Google trong khi sử dụng Tab trong ActionBar. Khi tôi tải trang tab, bản đồ tải tốt, nhưng tôi muốn lấy đối tượng bản đồ để tôi có thể căn giữa đối tượng, thêm điểm đánh dấu, v.v.Lấy Google Map Fragment trong onCreateView bằng cách sử dụng tab ActionBar

Có cách nào để làm điều này và nếu có, để cho tôi thấy như thế nào?

Dưới đây là mã cho trang theo thẻ của tôi ...

Phương pháp cụ thể Tôi đang làm việc với là public static class Map extends Fragment

public class Page3Activity extends FragmentActivity implements ActionBar.TabListener { 

    final Context context = this; 
    static GoogleMap map; 
    static MapView mMapView; 
    DatabaseHandler db = new DatabaseHandler(context); 

    AppSectionsPagerAdapter mAppSectionsPagerAdapter; 
    ViewPager mViewPager; 

    @SuppressLint("NewApi") 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.page3); 

     // Create the adapter that will return a fragment for each of the three primary sections 
     // of the app. 
     mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager()); 

     ColorDrawable colorDrawable = new ColorDrawable(); 
     final ActionBar actionBar = getActionBar(); 
     colorDrawable.setColor(0xff9ACC00); 
     actionBar.setBackgroundDrawable(colorDrawable); 
     actionBar.setTitle("Road Trip Calculator"); 
     actionBar.setSubtitle("Trip Information"); 
     actionBar.setDisplayShowTitleEnabled(true); 
     actionBar.setDisplayShowHomeEnabled(true); 
     actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE); 

    // Specify that the Home/Up button should not be enabled, since there is no hierarchical 
     // parent. 
     actionBar.setHomeButtonEnabled(false); 

     // Specify that we will be displaying tabs in the action bar. 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     // Set up the ViewPager, attaching the adapter and setting up a listener for when the 
     // user swipes between sections. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mAppSectionsPagerAdapter); 
     mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
      @Override 
      public void onPageSelected(int position) { 
       // When swiping between different app sections, select the corresponding tab. 
       // We can also use ActionBar.Tab#select() to do this if we have a reference to the 
       // Tab. 
       actionBar.setSelectedNavigationItem(position); 
      } 
     }); 

     // For each of the sections in the app, add a tab to the action bar. 
     for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) { 
      // Create a tab with text corresponding to the page title defined by the adapter. 
      // Also specify this Activity object, which implements the TabListener interface, as the 
      // listener for when this tab is selected. 
      actionBar.addTab(
        actionBar.newTab() 
          .setText(mAppSectionsPagerAdapter.getPageTitle(i)) 
          .setTabListener(this)); 
     } 

    } 

    /* 
    * ACTIONBAR TOP BAR CODE 
    */ 


    @Override 
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
    } 

    @Override 
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
     // When the given tab is selected, switch to the corresponding page in the ViewPager. 
     mViewPager.setCurrentItem(tab.getPosition()); 
    } 

    @Override 
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
    } 

    /** 
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary 
    * sections of the app. 
    */ 
    public static class AppSectionsPagerAdapter extends FragmentPagerAdapter { 

     public AppSectionsPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     //Switching between tabs 
     @Override 
     public Fragment getItem(int i) { 
      switch (i) { 
       case 0: 
        return new Map(); 
       case 1: 
        return new Directions(); 
       case 2: 
        return new poiList(); 
       case 3: 
        return new tripInfo(); 
       default: 
        i=i+1; 
      } 
      return null; 
     } 

     @Override 
     public int getCount() { 
      return 4; 
     } 

     @Override 
     public String getPageTitle(int position) { 
      String tabName = ""; 
      if(position == 0) { 
       tabName = "Map"; 
      } else if(position == 1) { 
       tabName = "Directions"; 
      } else if(position == 2) { 
       tabName = "POI List"; 
      } else if(position == 3) { 
       tabName = "Trip Info"; 
      } 
      return tabName; 

     } 
    } 

    /** 
    * Map 
    */ 
    public static class Map extends Fragment { 

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

      //I want to be able to do something like this... 
      /* 
      GoogleMap map; 
      SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map1); 
      map = fm.getMap(); 
      map.DOSOMETHING 
      */ 


      return rootView; 
     } 
    } 

    /** 
    * Directions 
    */ 

    public static class Directions extends Fragment { 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
       View rootView = inflater.inflate(R.layout.directionstab, container, false); 
       ((TextView) rootView.findViewById(android.R.id.text1)).setText(
       "directions"); 
      return rootView; 
     } 
    } 

    /** 
    * POI List 
    */ 

    public static class poiList extends Fragment { 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
       View rootView = inflater.inflate(R.layout.poitab, container, false); 
       ((TextView) rootView.findViewById(android.R.id.text1)).setText(
       "poiList"); 
      return rootView; 
     } 
    } 

    /** 
    * Trip Info 
    */ 

    public static class tripInfo extends Fragment { 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.tripinfotab, container, false); 
      ((TextView) rootView.findViewById(android.R.id.text1)).setText(
      "Trip Info"); 
      return rootView; 
     } 
    } 

} 

Đây là tập tin XML của tôi (maptab.xml) mà tôi có bản đồ của tôi đoạn trong ...

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

    <fragment 
    android:id="@+id/map1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    class="com.google.android.gms.maps.SupportMapFragment" 
    /> 

</LinearLayout> 

SOLVED

public static class Map extends Fragment { 
     MapView mapView; 
     GoogleMap map; 

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

      // Gets the MapView from the XML layout and creates it 
      mapView = (MapView) v.findViewById(R.id.mapview); 
      mapView.onCreate(savedInstanceState); 

      // Gets to GoogleMap from the MapView and does initialization stuff 
      map = mapView.getMap(); 
      //map.getUiSettings().setMyLocationButtonEnabled(false); 
      //map.setMyLocationEnabled(true); 
      map.addMarker(new MarkerOptions().position(new LatLng(50.167003,19.383262))); 


      // Needs to call MapsInitializer before doing any CameraUpdateFactory calls 
      try { 
       MapsInitializer.initialize(this.getActivity()); 
      } catch (GooglePlayServicesNotAvailableException e) { 
       e.printStackTrace(); 
      } 

      // Updates the location and zoom of the MapView 
      CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10); 
      map.animateCamera(cameraUpdate); 

      return v; 
     } 

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

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

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

Nó sẽ hữu ích hơn cho người khác nếu bạn đề cập rằng bạn thay thế SupportMapFragment của bạn trong maptab.xml bằng MapView. – tsil

Trả lời

2

Bạn có thể tham chiếu đến đoạn có chứa Bản đồ của bạn bằng cách sử dụng mViewPager.getItem(0) và sau đó truyền đến Map đoạn và sử dụng nó theo bất kỳ cách nào bạn muốn. Nhưng điều này thực sự không phải là thiết kế rất tốt để hoạt động trên mảnh từ bên ngoài. Vì vậy, tôi khuyên bạn nên thực hiện tất cả logic nghiệp vụ liên quan đến bản đồ bên trong đoạn Map, theo cách này bạn không cần phải giải quyết loại vấn đề đó, mà bạn đã đăng và tất cả logic nghiệp vụ của bạn được đóng gói bên trong một đoạn.

+0

Tôi cần vẽ một PolyLine trên bản đồ và thêm điểm đánh dấu tùy thuộc vào lựa chọn của người dùng trên các trang trước. Đó là lý do tại sao tôi cần phải làm điều đó thông qua mã. Bạn có thể cho tôi một ví dụ về những gì bạn đang nói về? Cảm ơn. – Zack

+0

Vâng, thực sự tôi đã làm, đây là đầy đủ hơn '((Bản đồ) mViewPager.getItem (0)). SomeMethod()', trong đó 'someMethod()' có thể trả lại cho bạn một tham chiếu đến 'GoogleMap', mà bạn có thể làm bất cứ điều gì. – Desert

+0

Tôi đoán tôi đang bối rối. Tôi có '((Bản đồ) mViewPager.getItem (0))' nhưng nó muốn tôi đặt nó thành một biến. Làm cách nào tôi có thể lấy mục đó và đặt nó bằng googlemap? – Zack

Các vấn đề liên quan