簡體   English   中英

Google會在actionbarsherlock標簽中進行映射

[英]Google maps in an actionbarsherlock tab

我想讓谷歌地圖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文件來膨脹(如果它應該是什么樣子?) - 或者我應該以編程方式創建我的視圖?

我現在已經在很多應用程序中完成了這個。 您只需創建自己的MapFragment,而不是擴展SupportMapFragment。 您可以擁有自己的布局,其中包含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>

你可以用這種方式。

 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密鑰要求所有的東西。如果您收到任何錯誤日志,然后作為評論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM