在ActionBarSherlock选项卡中使用Google地图

7

我正在尝试在我的应用程序中使用谷歌地图v2。我看过几个示例,展示了如何在活动中打开SupportMapFragment。这个想法是你的活动将调用setContentView(R.layout.map_layout);其中map_layout.xml链接到带有以下行的片段:

android:name="com.google.android.gms.maps.SupportMapFragment"
        xmlns:map="http://schemas.android.com/apk/res-auto"

"name="这一行实际上表示“这个布局由类型为'SupportMapFragment'的片段来控制”。

我的问题是,我试图让地图显示在一个带有选项卡的活动中(使用actionbarsherlock实现)。这意味着与选项卡选择相对应的任何片段都必须实现TabListener。但SupportMapFragment没有。因此,现在我需要创建一个新的片段,如下所示:

public class MyMapFragmentWithTabListener extends SupportMapFragment implements TabListener
{

但是,现在我对于如何编写MapFragmentWithTabListener的onCreateView方法感到困惑...我应该要填充一些布局吗?当然,我不能完全填充与示例中相同的map_layout.xml文件,因为它已经声明由SupportMapFragment控制,而在这个实现中应该由MyMapFragmentWithTabListener控制 - 我需要一个略微不同的xml文件来填充(如果需要,它应该长什么样?)-或者我应该通过程序创建我的视图?


为什么需要使用片段来实现TabListener? - AMerle
你总是可以使用原始的 MapView。有没有必须使用 SupportMapFragment 的理由? - Izydorr
我认为以下帖子应该会对你有所帮助。https://dev59.com/wGYr5IYBdhLWcg3wYZOD - blganesh101
2个回答

6
我已经在很多应用中做过这个了。不要扩展SupportMapFragment,而是创建自己的MapFragment。您可以拥有自己的布局,其中包含一个MapView视图。关键是将Fragment的生命周期事件路由到MapView,就可以轻松实现了。
以下是示例代码:
MapFragment:
package com.example.testapplication;

import java.util.concurrent.TimeUnit;

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;


public class TestMapFragment extends Fragment {

    private MapView mMapView;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_fragment, container, false);

        mMapView = (MapView) view.findViewById(R.id.mapview);

        // inflat and return the layout
        mMapView.onCreate(savedInstanceState);
        mMapView.onResume();// needed to get the map to display immediately

        try {
            MapsInitializer.initialize(getActivity());
        } catch (GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }
        GoogleMap googleMap = mMapView.getMap();
        googleMap.getUiSettings().setMyLocationButtonEnabled(true);
        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    /*
     * Using a mapview in a fragment requires you to 'route'
     * the lifecycle events of the fragment to the mapview
     */
    @Override
    public void onResume() {
        super.onResume();
        if (null != mMapView)
            mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (null != mMapView)
            mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != mMapView)
            mMapView.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (null != mMapView)
            mMapView.onSaveInstanceState(outState);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        if (null != mMapView)
            mMapView.onLowMemory();
    }
}

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.google.android.gms.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        map:uiZoomControls="false" />
</RelativeLayout>

一个好主意。在Fragment的onResume中,我调用了“((CustomSupportMapFragment) getChildFragmentManager().findFragmentById(R.id.fm_map)).onResume();”,这对我很有效。谢谢! - Anh Duy

0

你可以用这种方式。

 public class NewActivity extends FragmentActivity {
 private GoogleMap mMap;
 SupportMapFragment mapFragment;

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                this.requestWindowFeature(Window.FEATURE_NO_TITLE);
                setContentView(R.layout.activit);

        mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); 
        if(isGooglePlayServicesIsInstalled(mContext)){
            mMap = mapFragment.getMap();
            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            mMap.getUiSettings().setCompassEnabled(true);
            mMap.getUiSettings().setZoomControlsEnabled(true);
        }else{
              //display any toast message
                //Global.Toast("Please First install Google Maps");
            }



public static boolean isGooglePlayServicesIsInstalled(Context context){
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
        if (resultCode == ConnectionResult.SUCCESS) 
            return true;
         else 
             return false;
    }

请检查所有的权限API密钥必需的所有内容。如果您遇到任何错误日志,请将其作为评论放置。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接