在我的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。 –