这里是地图 - 在TabLayout的片段中使用MapFragment

时间:2017-02-10 17:33:02

标签: android android-fragments here-api

我的应用有3个标签,其中一个我要显示地图(此处地图,而不是Google地图)。我能够让地图在Activity中完美地工作,但在Fragment Class中,当我尝试将Fragment视图转换为MapFragment时,会抛出错误。

MapFragment mapFragment = (MapFragment) 
                getFragmentManager().findFragmentById(R.id.mapfragment);

错误: Inconvetible类型:无法将android.support.v4.app.support转换为com.here.android.mpa.mapping.MapFragment

如果我错了,请纠正我,但发生这种情况的原因是因为我们无法在android.support中使用android.app.Fragment(用于在HERE MAPS DOC中指示显示地图) .v4.app.Fragment(用于TabsLayout)。

我使用Google地图发现了许多与此错误相关的问题。但在使用 HERE MAPS 时,只有两个(firstsecond)有同样的错误,而且没有一个真正有助于解决问题。

在Google地图中,您可以使用SupportMapFragment(),但此方法仅适用于Google地图。使用Here Maps有没有解决方案?也许是一种达到同一目标的不同方法?或者我在TabLayout中实现此地图时错过了什么?

任何帮助将不胜感激!

我的代码:

MapFragment.java

  public class Map extends Fragment {
        public Map() {}
        public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_map, container, false);
            MapFragment mapFragment = (MapFragment) 
                    getFragmentManager().findFragmentById(R.id.mapfragment);
            mapFragment.init(new OnEngineInitListener() {
                @Override
                public void onEngineInitializationCompleted(
                        OnEngineInitListener.Error error) {
                    if (error == OnEngineInitListener.Error.NONE) {
                        com.here.android.mpa.mapping.Map map = mapFragment.getMap();
                    } else {
                        System.out.println("ERROR: Cannot initialize MapFragment");
                    }
                }
            };
            return view;
        }

fragment_map.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.modulos.tabsMenu.Config">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/mapFragmentContainer"
        android:layout_weight="1"
        android:orientation="vertical"
        android:gravity="center"
        android:background="#aaa" >

        <fragment
            class="com.here.android.mpa.mapping.MapFragment"
            android:id="@+id/mapfragment"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:gravity="center"
        android:background="#aaa" >
            <!-- other things here -->
    </LinearLayout>
</FrameLayout>

2 个答案:

答案 0 :(得分:3)

由于HERE Maps SDK仅适用于API Level 15及更高版本,因此不支持兼容性片段(当您以较低的API级别为目标时,主要需要这些片段)。 在某些情况下,即使您使用Android 4或更新版本作为目标,您仍然希望处理支持片段,在这种情况下,您必须使用HERE SDK的MapView并将其嵌入您自己的布局中。 请参阅此代码示例,其中还使用了MapView:https://tcs.ext.here.com/sdk_examples/MapDownloader.zip

所以,它看起来像这样:

在布局中添加MapView:

<com.here.android.mpa.mapping.MapView
    android:id="@+id/ext_mapview"
    android:visibility="visible"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

然后在你的代码中:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapView = (MapView) findViewById(R.id.ext_mapview);
        MapEngine.getInstance().init(this, engineInitHandler);
    }

  private OnEngineInitListener engineInitHandler = new OnEngineInitListener() {
        @Override
        public void onEngineInitializationCompleted(Error error) {
            if (error == Error.NONE) {
                map = new Map();
                mapView.setMap(map);
                // more map initial settings
            } else {
                Log.e(TAG, "ERROR: Cannot initialize MapEngine " + error);
            }
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        MapEngine.getInstance().onResume();
        if (mapView != null) {
            mapView.onResume();
        }
    }

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

处理MapEngine和MapView暂停和恢复非常重要,否则会导致性能不佳。

答案 1 :(得分:0)

当然这是一种愚蠢的问题,但你是否检查过你是否使用了正确包装中的MapFragment?

您必须使用此类 com.here.android.mpa.mapping.MapFragment ,可能是因为错误导入了此 com.google.android.gms.maps.MapFragment < /强>