androidkotlinhere-apisupportmapfragment

mapping.MapView cannot be cast to mapping.SupportMapFragment


I just implement here sdk into my project for first test but i cant get anywhere and on here website there is no kotlin support

try{
            mapfragment!!.init { error ->
                if (error == OnEngineInitListener.Error.NONE) {
                    // retrieve a reference of the map from the map fragment

                    var fragmentMap = mapfragment.map

                    map = mapfragment.map

                    // Set the map center to the Vancouver region (no animation)
                    map!!.setCenter(GeoCoordinate(49.196261, -123.004773, 0.0), Map.Animation.NONE)
                    // Set the zoom level to the average between min and max
                    map!!.zoomLevel = (map!!.maxZoomLevel + map!!.minZoomLevel) / 2
                } else {
                    println("ERROR: Cannot initialize Map Fragment")
                }
            }

        }
        catch
            (e: Exception)
        {
            Log.e("exception","map",e)
        }catch
            (ej: java.lang.Exception)
        {
            Log.e("exception","map",ej)
        }

this part mapfragment!!.init is giving me exception

java.lang.ClassCastException: com.here.android.mpa.mapping.MapView cannot be cast to com.here.android.mpa.mapping.SupportMapFragment

this my layout

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

Solution

  • I think you're missing the SupportMapFragment object, as @David mentioned. You need to link it, basically.

    Take a look at mine, I got mine working in Kotlin using this:

    var map: Map? = null
    var mapRoute: MapRoute? = null
    
    //get mapfragment
    fun getSupportMapFragment(): SupportMapFragment {
        return getSupportFragmentManager().findFragmentById(R.id.mapfragment) as SupportMapFragment
    }
    
    //initializing the map view with default values
    fun initializeMap() {
        val mapFragment = getSupportMapFragment()
        mapFragment.init { error ->
            if (error == OnEngineInitListener.Error.NONE) {
                // retrieve a reference of the map from the map fragment
                map = mapFragment.getMap()
                // Set the map center coordinate (no animation) - just to initialize map
                map!!.setCenter(GeoCoordinate(0.0, 0.0, 0.0), Map.Animation.NONE)
                // Set the map zoom level to the average between min and max (no animation)
                map!!.setZoomLevel((map!!.getMaxZoomLevel() + map!!.getMinZoomLevel()))
    
            } else {
                println("ERROR: Cannot initialize Map Fragment")
                //Log.e(LOG_TAG, "Cannot initialize SupportMapFragment ($error)")
            }
        }
    }