简体   繁体   English

将GoogleMaps V2放在TabsPagerAadapter中?

[英]Put GoogleMaps V2 in TabsPagerAadapter?

I am working on GoogleMaps v2 and finished it but when put it in TabsPagerAdpter this error appeard ( Type missmatch can't convert from MapActivity to Fragment ) 我正在使用GoogleMaps v2并将其完成,但是将其放在TabsPagerAdpter时会TabsPagerAdpter此错误( 类型不 TabsPagerAdpter 无法从MapActivity转换为Fragment

I want to convert FragmentActivity to Fragment in Map to support TabsPageradapter where tabsPagerAdaper doesn't deal with FragmentActivity 我想将FragmentActivity转换为Map中的Fragment以支持TabsPageradapter ,其中tabsPagerAdaper不会处理FragmentActivity

I try to convert the MapActivity class to be extended Fragment but i can't. 我尝试将MapActivity类转换为可扩展的Fragment,但我不能。

MapActivity class MapActivity类

public class MapActivity extends FragmentActivity  {
private static final int GPS_ERRORDIALOG_REQUEST = 9001;

GoogleMap Gmap;
Marker marker;

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


    if (servicesOK()) {
        setContentView(R.layout.ogenia);
        if (initMap()){
            Toast.makeText(this,"Ready to map",Toast.LENGTH_SHORT).show();

            gotoLocation();
        }
        else {
            Toast.makeText(this,"Map not available",Toast.LENGTH_SHORT).show();

        }
    }


}

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

    if(isAvailable == ConnectionResult.SUCCESS){
        return true;
    }
    else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)){
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this,GPS_ERRORDIALOG_REQUEST);
        dialog.show();
    }
    else {
        Toast.makeText(this,"can't connect to Google Play Services",Toast.LENGTH_SHORT).show();
    }
    return false ;
}
private boolean initMap(){
    if (Gmap == null){
        SupportMapFragment mapFrag 
        =(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
        Gmap = mapFrag.getMap();
    }
    return(Gmap != null) ;
}
private void gotoLocation() {
    // Enable my location layer of Google map

            Gmap.setMyLocationEnabled(true);
            // get LocationManger object from system service LOCATION_SERVICE
            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

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

            // Get the name of best provider
            String provider = locationManager.getBestProvider(criteria, true);
            // Get Current Location
            Location myLocation = locationManager.getLastKnownLocation(provider);

            // set map type
            Gmap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            //if (myLocation != null) {

            // Get latitude of the current location

            double latitude = myLocation.getLatitude();

            // Get longitude of the current location

            double longitude = myLocation.getLongitude();

            // create latlng object for the current location

            LatLng latlng = new LatLng(latitude, longitude);

            // show the current location in Google map

            Gmap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
            // zoom in Google map
            Gmap.animateCamera(CameraUpdateFactory.zoomTo(15));




        /*  if (marker != null) {
                marker.remove();
            }
            Gmap.addMarker(new MarkerOptions().position(
                    new LatLng(latitude, longitude)).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_marker))
                .draggable(true).title("current location")); */


}

 // private void searchLocation(double lat, double lng)
//       {
//  LatLng ll = new LatLng(lat, lng);
//  Gmap.moveCamera(CameraUpdateFactory.newLatLng(ll));
            // zoom in Google map
//          Gmap.animateCamera(CameraUpdateFactory.zoomTo(20));
//}



private void hideSoftKeyboard(View v) {
    // TODO Auto-generated method stub
    InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

}





}

}

TabsPagerAdapter class : error appears in the case 0 ( Type missmatch can't convert from MapActivity to Fragment as MapActivity class extended FagmentActivity ) TabsPagerAdapter类:在情况0下出现错误(类型不匹配无法作为MapActivity类扩展的FagmentActivity从MapActivity转换为Fragment)

public class TabsPagerAdapter extends FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0:
        // Map fragment activity
         return new MapActivity();
    case 1:
        // Directions fragment activity
        return new Directions();
    case 2:
        // Places fragment activity
        return new Places();
    }

    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 3;
}

}

please help me 请帮我

One Suggestion For What You are Doing.Putting Google Map in TabView is bad thinking if the TabView contains the Swiping Functionality.So Both Swipe will Conflict ie the Map Swipe & TabView. 一个关于您正在做什么的建议。如果TabView包含滑动功能,则将Google Map放入TabView是不好的想法,因此两次滑动都会冲突,即Map Swipe和TabView。 Hope this will redirect you to correct Path.Thnx 希望这会将您重定向到正确的Path.Thnx

您不能将“活动”转换为片段,但是可以使用地图库提供的MapFragment。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM