无法在Android应用程序的地图中查看标记和动画相机位置

时间:2014-10-01 09:33:25

标签: android google-maps android-fragments

您好我正在开发一个学校应用程序,因为我使用导航抽屉从一个部分移动到另一个部分。对于我使用片段的所有部分。

在about_us部分中,我想显示地图和文本视图。

1)其中,我可以查看地图和文本视图,但它没有在我设置的经度和经度的确切位置显示标记,也没有显示相机更新动画。它只是显示世界地图。

2)如果我从about_us部分移动到另一部分例如。(事件)那么如果我再次回到about_us它不打开页面它的显示不幸的是应用程序被关闭了。 (我不知道我的onResume方法发生了什么)

这是我第一次使用Navigation抽屉和Fragment。我不知道为什么会这样。这个相同的代码在活动中运行良好,但不在片段中。请帮我解决这个问题。

public class AboutUsFragment extends Fragment {

public AboutUsFragment(){}

private GoogleMap gmap;

static final double latitude = 13.12615;
static final double longitude = 80.21932;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_about_us, container, false);

    initilizeMap();

    return rootView;


}


private void initilizeMap()
{
    if (gmap == null) {
        gmap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        // check if map is created successfully or not
        if (gmap == null) {
            Toast.makeText(getActivity(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();

            //To enable compass
            gmap.getUiSettings().setCompassEnabled(true);

            //To locate the school - Creating Marker
            MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Everwin Vidhyashram");

            // adding marker
            gmap.addMarker(marker);

            //We assigned latitude and longitude in Coordinate
            LatLng coordinate = new LatLng(latitude, longitude);
            CameraUpdate schoolLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 5);
            gmap.animateCamera(schoolLocation);

            CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(coordinate)         // Sets the center of the map to Mountain View
            .zoom(17)                   // Sets the zoom
            .bearing(90)                // 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
            gmap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        }
    }
}

@Override
public void onResume() {
    super.onResume();
    //initilizeMap();

    if (gmap == null) {
        gmap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        // check if map is created successfully or not
        if (gmap == null) {
            Toast.makeText(getActivity(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
        }
    }
}
}

2 个答案:

答案 0 :(得分:0)

从if(gmap == null){}中删除您的代码。如果未创建地图,则会发生这种情况。

private void initilizeMap()
{
    if (gmap == null) {
        gmap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        // check if map is created successfully or not
        if (gmap == null) {
            Toast.makeText(getActivity(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();


        }

//To enable compass
            gmap.getUiSettings().setCompassEnabled(true);

            //To locate the school - Creating Marker
            MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Everwin Vidhyashram");

            // adding marker
            gmap.addMarker(marker);

            //We assigned latitude and longitude in Coordinate
            LatLng coordinate = new LatLng(latitude, longitude);
            CameraUpdate schoolLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 5);
            gmap.animateCamera(schoolLocation);

            CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(coordinate)         // Sets the center of the map to Mountain View
            .zoom(17)                   // Sets the zoom
            .bearing(90)                // 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
            gmap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    }
}

答案 1 :(得分:0)

在你的create activity中调用这样的getMap方法。

public class ShowMap extends FragmentActivity implements LocationListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_location_map);

  getMap();
 } 

}

public void getMap(){




      int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

        // Showing status
        if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available

            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
            dialog.show();

        }else { // Google Play Services are available

            // Getting reference to the SupportMapFragment of activity_main.xml
            SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);


            // Getting GoogleMap object from the fragment
            googleMap = fm.getMap();

            // Enabling MyLocation Layer of Google Map
           googleMap.setMyLocationEnabled(true);


        }





}

将此用于你的onLocationChanged map方法。

@Override
public void onLocationChanged(Location location ) {


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

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

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

 // Showing the current location in Google Map
 googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

 // Zoom in the Google Map
 googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

}