片段中的MapFragment

时间:2014-02-12 13:33:23

标签: android android-fragments google-maps-android-api-2

我正在尝试使用片段实现GoogleMap api - 用于滑动条菜单。 这是片段代码

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" />

我已经尝试了this solution.,但是eclipse中的这一行是一个错误

  map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();
  

无法从Fragment转换为SupportMapFragment

0 个答案:

没有答案