在我的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。
如果你想隐藏/显示的东西而不需要重新加载整个地图(你会想的是,最终) ,引用Marker/Polygon/Polyline(不是MarkerOptions等),并使用'setVisible'来切换false/true。 –