3

My application has 3 tabs and in one of them I want to display a map(HERE MAPS, not Google Maps). I was able to get the map to work perfectly in an Activity, but in Fragment Class when I try to cast the Fragment view to the MapFragment an error is thrown.

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

Error: Inconvetible types: cannot cast android.support.v4.app.support to com.here.android.mpa.mapping.MapFragment

Please correct me if Im wrong, but the reason for this to happen is because we can´t use android.app.Fragment(used to display the map, as instructed in HERE MAPS DOC) in an android.support.v4.app.Fragment(used for the TabsLayout).

I found many questions related to this error using Google Maps. but just two(first,second)about the same error when using HERE MAPS, and none really helped to solve the problem.

In google Maps you can use SupportMapFragment(), but this mehod only works for Google Maps. Is there any solution for this using Here Maps? maybe a different approach to reach the same goal? or am I missing something when implementing this Here Maps in a 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>
Community
  • 1
  • 1
Luana
  • 33
  • 7

2 Answers2

3

Since the HERE Maps SDK only works with API Level 15 and above, there's no support for compatibility fragments (that you mainly need when you target lower API levels). In some cases you still want to deal with support fragment even if you have Android 4 or newer as target, in those cases you have to use the the MapView of the HERE SDK and embedded it in your layout on your own. See this code example where also MapView is used: https://tcs.ext.here.com/sdk_examples/MapDownloader.zip

So, it will look like this:

Add in your layout the 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" />

And 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's important that you handle the MapEngine and MapView Pause and Resume, otherwise you would get poor performance results.

Marco
  • 2,190
  • 15
  • 22
0

Surely is a silliness ask this, but do you check if are you using MapFragment from the right package?

You must be using this class com.here.android.mpa.mapping.MapFragment, maybe by error you import this one com.google.android.gms.maps.MapFragment.