无法从Fragment转换为Mapfragment

时间:2014-10-07 05:50:40

标签: android

我想在Android中制作地图,这是为了我的Skripsi,我在这里下载源代码https://github.com/ajaswal/GoogleMapsV2我strat打开项目,但有问题这是我的问题,我有警告关于无法从Fragment转换为MapFragment,这个问题在第27行。

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

所以请帮助我......

package com.example.googlemaps;

import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;

public class MainActivity extends Activity {
public final LatLng LOCATION_BURNABY= new LatLng(49.27645, -122.917587);
public final LatLng LOCATION_SURREY= new LatLng(49.187500, -122.849000);

private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    if (map != null) {
        setUpMap();
    }
}

private void setUpMap() {
    // Enable MyLocation layer of Google map
            map.setMyLocationEnabled(true);

            // Get Location manager object from System service LOCATION_SERVICE
            LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE);

            // Create a criteria object to retrieve provider
            Criteria criteria = new Criteria();

            // Get the name of the best provider

            String provider = locManager.getBestProvider(criteria, true);

            // Get current location
            Location myLocation = locManager.getLastKnownLocation(provider);

            // Set map type
            map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

            // Get the latitude of the current location
            double latitude = myLocation.getLatitude();

            // Get the longitude of the current location
            double longitude = myLocation.getLongitude();

            // Create a LatLng object for the current location
            LatLng LOCATION_CURRENT = new LatLng(latitude, longitude);

            // Show the current location in Google map
            map.moveCamera(CameraUpdateFactory.newLatLng(LOCATION_CURRENT));

            // Zoom in the google map
            CameraUpdate camUpdate = CameraUpdateFactory.newLatLngZoom(
                    LOCATION_CURRENT, 15);
            map.animateCamera(camUpdate);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void onClick_Burnaby(View v){
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_BURNABY, 13);
    map.animateCamera(update);
}

public void onClick_Surrey(View v){
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_SURREY, 14);
    map.animateCamera(update);
}}

2 个答案:

答案 0 :(得分:0)

使用FragmentActivity代替活动

示例 -

public class MapScreen extends FragmentActivity{

    private GoogleMap map;
    private Location location;
    private ArrayList<LatLng> mLocationsLatLngList;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.map_screen);


        map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapfragment)).getMap();
        CameraPosition cameraPosition = new CameraPosition.Builder()
        .target(Your_CurrentLocation).zoom(13).bearing(0) // Sets the orientation of the camera to east
        .tilt(30)    // Sets the tilt of the camera to 30 degrees
        .build();    // Creates a CameraPosition from the builder
        map.setBuildingsEnabled(true);
        map.setMyLocationEnabled(true);
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));   

    }   
}

xml -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/mapfragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        class="com.google.android.gms.maps.SupportMapFragment" />

</LinearLayout>

答案 1 :(得分:0)

您在SupportMapFragment中使用的是XML,因此您必须使用getSupportFragmentManager代替getFragmentManager

使用此代码获取FragmentMap

SupportMapFragment fr = (SupportMapFragment)this.getSupportFragmentManager().findFragmentById(R.id.mapfragment);
GoogleMap Map = fr.getMap();

您的Activity必须延长FragmentActivity