Android 在片段中映射片段
我试图在使用片段的同时实现GoogleMapAPI——用于幻灯片菜单。 这是片段代码Android 在片段中映射片段,android,android-fragments,google-maps-android-api-2,Android,Android Fragments,Google Maps Android Api 2,我试图在使用片段的同时实现GoogleMapAPI——用于幻灯片菜单。 这是片段代码 public class MapFragment extends Fragment { private GoogleMap map; static final LatLng HAMBURG = new LatLng(53.558, 9.927); static final LatLng KIEL = new LatLng(53.551, 9.993); public MapFragment(){} publi
public class MapFragment extends Fragment {
private GoogleMap map;
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
public MapFragment(){}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_map, container, false);
map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
//...
//...
return rootView;
}
布局xml
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
无法从片段强制转换为SupportMapFragment
试试这个
map=((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap()
已经尝试过了,但谢谢,问题在于eclipse中的自动导入。我编辑了我的文章。或者没有。这不起作用,因为它替换了扩展片段。您的sdk的最低版本是什么?我在AndroidManifest中设置的最低版本是11
map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();