【问题标题】:Google maps in an actionbarsherlock tabactionbarsherlock 选项卡中的 Google 地图
【发布时间】:2016-01-14 00:13:57
【问题描述】:

我正在尝试让谷歌地图 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?
  • 您始终可以使用原始MapView。你有什么理由必须使用SupportMapFragment
  • 我认为以下帖子应该对您有所帮助。 stackoverflow.com/questions/13721929/…

标签: android xml


【解决方案1】:

我现在已经在很多应用程序中做到了这一点。您只需创建自己的 MapFragment,而不是扩展 SupportMapFragment。您可以拥有自己的布局,其中包含 MapView 视图。关键是把Fragment的生命周期事件路由到MapView,然后bobs你的大叔。

这里有一些示例代码:

地图片段

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>

【讨论】:

  • 一个好主意。在片段的 onResume 中,我调用了“((CustomSupportMapFragment) getChildFragmentManager() .findFragmentById(R.id.fm_map)).onResume();”它对我有用。谢谢!
【解决方案2】:

你可以这样使用。

 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密钥需要所有的东西。如果你得到然后将任何错误日志作为注释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 2012-09-08
    相关资源
    最近更新 更多