Android – here map – uses mapfragment in the fragment of tablayout

My application has three tags, one of which is to display maps (here maps, not Google Maps). I can make maps run perfectly in activity, but in fragment class, an error will be raised when I try to convert fragment view to mapfragment

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

Error: Incredible type: unable to convert android.support.v4.app.support to com.here.android.mpa.mapping.mapfragment

If I'm wrong, please correct me, but this happens because we can't use android.app.fragment (for displaying maps, as indicated in here maps DOC) in android.support.v4.app.fragment (for tabslayout)

I found many problems related to this error using Google maps. However, when using here maps, only two (first, second) have about the same error, and none of them is really helpful to solve the problem

In Google maps, you can use supportmapfragment (), but this method is only applicable to Google maps. Is there any solution using "here map"? Maybe there are different ways to achieve the same goal? Or will something be lost when implementing this here maps in tablayout?

Any help will be appreciated!

My code:

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>

resolvent:

Because the here maps SDK is only applicable to API level 15 and higher, it does not support compatibility fragments (which are mainly required when locating lower API levels). In some cases, you still want to process support fragments even if you target Android 4 or higher. In these cases, You must use the MapView of the here SDK and embed it into the layout. See the following code example, in which MapView is also used: https://tcs.ext.here.com/sdk_ examples/MapDownloader.zip

Therefore, it will be as follows:

To add a MapView to a layout:

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

Then in your code:

 @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();
    }

It is important to handle the pause and resume of MapEngine and MapView, otherwise you will get poor performance results

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>