谷歌在actionbarsherlock标签映射 [英] Google maps in an actionbarsherlock tab

查看:98
本文介绍了谷歌在actionbarsherlock标签映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让谷歌地图V2在我的应用程序工作。我所看到的告诉你只要打开SupportMapFragment一个活动里面的几个例子。这个想法是,你的活动将调用的setContentView(R.layout.map_layout); 其中map_layout.xml链接与线片段:

I am trying to get google maps v2 working in my app. I have seen several examples showing how you can open up SupportMapFragment inside an activity. The idea being that your activity will call setContentView(R.layout.map_layout); where map_layout.xml links to the fragment with the lines:

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

名称=行有效地说:这个布局是由类型的片段SupportMapFragment控制。

The "name=" line effectively says that "this layout is to be controlled by a fragment of type 'SupportMapFragment'".

我的复杂之处在于我试图去出现在标签的活动(与actionbarsherlock实现)地图。这意味着,无论片段对应一个标签选择必须实行TabListener。但SupportMapFragment没有。所以,现在presumably我需要建立像一个新片段这样:

My complication is that I am attempting to get the map to appear in an activity with tabs (implemented with actionbarsherlock). This means that whatever fragment corresponds to a tab selection must implement a TabListener. But SupportMapFragment doesn't. So now presumably I need to create a new fragment like so:

public class MyMapFragmentWithTabListener extends SupportMapFragment implements TabListener
{

但现在我已经得到了所有困惑怎么写MapFragmentWithTabListener,特别onCreateView ......我应该夸大一些布局的内容是什么?我肯定不能精确地膨胀从​​实施例相同map_layout.xml因为已经声明,它是由SupportMapFragment控制,而在本实施,应该由MyMapFragmentWithTabListener控制 - 是否需要一个稍微不同的xml文件充气(如果因此,应该看起来像) - ?或者我应该通过程序创建我的观点

But now I have got all confused about how to write the contents of MapFragmentWithTabListener in particular onCreateView... should I be inflating some layout? Surely I can't be inflating exactly the same map_layout.xml from the examples because that already declares that it is controlled by SupportMapFragment, whereas in this implementation it should be controlled by MyMapFragmentWithTabListener - do I need a slightly different xml file to inflate (if so, what should it look like?) - or should I be creating my view programatically?

推荐答案

我在相当多的应用程序现在做到了这一点。而不是延长SupportMapFragment的,你只是创建自己的MapFragment。你可以有自己的布局,它里面一个图形页面视图。关键是路由片段的MapView的生命周期事件,和鲍勃你叔叔。

I've done this in quite a few applications now. Instead of extending SupportMapFragment, you just create your own MapFragment. You can have your own layout, with a MapView view inside of it. The key is to route the lifecycle events of the Fragment to the MapView, and bobs your uncle.

下面有一些例子code:

Heres some example code:

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" />
</RelativeLaout>

这篇关于谷歌在actionbarsherlock标签映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆