2014-01-16 148 views
0

在我的Android應用程序,我使用谷歌地圖API第2版和導航抽屜訪問各種類別,如文化,美食,...刷新/重載谷歌地圖API V2

默認情況下,地圖加載的所有項目但是如果我選擇一個類別,我希望地圖升級。

我試過從我的mainactivity調用一個片段的方法,但我創建了dinamically片段,我不能訪問「findFragmentById」。這是我的代碼:

MapaFragment.java(地圖片段)

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     if (container != null) { 
      container.removeAllViews(); 
     } 

     rootView = inflater.inflate(R.layout.fragment_mapa, container, false); 
     listaLugares = ((ListaLugares)getActivity().getApplicationContext()); 

     // obtengo el mapa  
     mapa = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 

     // Move the camera instantly to my position with a zoom of 15. 
     LatLng miPosicion = new LatLng(main.obtLatitud, main.obtLongitud); 
     mapa.moveCamera(CameraUpdateFactory.newLatLngZoom(miPosicion , 14)); 

     // Zoom in, animating the camera. 
     mapa.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null); 

     // Add marker to the map 
     addItemsToMap(); 


     return rootView; 

    } 

    public void addItemsToMap() { 
     mapa.clear(); 
     if (listaLugares.lista.isEmpty()) { 
      listaLugares.readPlaces(5000, 0, listaLugares.idCategoria); 
     } 

     LatLng miPosicion = new LatLng(main.obtLatitud, main.obtLongitud); 
     mapa.addMarker(new MarkerOptions() 
      .position(miPosicion) 
      .title("Mi posición") 
      .icon(BitmapDescriptorFactory.fromResource(R.drawable.location_icon))); 

     for (int i = 0; i < listaLugares.lista.size(); i++) { 

      LatLng posItem = new LatLng(listaLugares.lista.get(i).latitud,listaLugares.lista.get(i).longitud); 

      mapa.addMarker(new MarkerOptions() 
       .position(posItem) 
       .title(listaLugares.lista.get(i).nombre) 
       .snippet(listaLugares.lista.get(i).descripcion) 
       .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))); 
      Log.v("MAPA", "Marker " + i + ": " + listaLugares.lista.get(i).nombre); 
     } 
    } 

fragment_mapa.xml(包含地圖片段的佈局)

<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" 
    tools:context=".MainActivity" > 

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

</RelativeLayout> 

注:MainActivity.java中,我創建了導航抽屜,當我選擇一個項目時,它應該刷新m AP。

+0

如果你想隱藏/顯示的東西而不需要重新加載整個地圖(你會想的是,最終) ,引用Marker/Polygon/Polyline(不是MarkerOptions等),並使用'setVisible'來切換false/true。 –

回答

2

使用不同的簽名更改addItemsToMap()方法以將抽屜的選定位置作爲該方法的參數傳遞。所以,你將有:

public void addItemsToMap(int position) { 
    switch(position) { 
      case 0 : // show all 

        break; 
      case 1 : // show markers of menu 1 

        break; 
      case 2 : // show markers of menu 2 

        break; 
      /* etc ... */ 
    }   
} 

現在您onCreateView()方法,調用addItemsToMap(0)顯示你所有的標記在

+0

非常感謝你!你的答案非常有用。我只需要將參數映射傳遞給MainActivity,但我創建了一個全局參數'map',然後我解決了它。 – Yeray