2014-01-16 147 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