mapping.MapView无法强制转换为mapping.SupportMapFragment

问题描述 投票:1回答:2

我只是在这里实施sdk进入我的项目进行第一次测试,但我无法到达任何地方,在这里网站上没有kotlin支持

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)
        }

这部分mapfragment!!.init给了我例外

java.lang.ClassCastException:com.here.android.mpa.mapping.MapView无法强制转换为com.here.android.mpa.mapping.SupportMapFragment

这是我的布局

 <fragment
        class="com.here.android.mpa.mapping.SupportMapFragment"
        android:id="@+id/mapfragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
android kotlin here-api supportmapfragment
2个回答
1
投票

我认为你错过了SupportMapFragment对象,正如@David所提到的那样。基本上你需要链接它。

看看我的,我让我在Kotlin工作使用这个:

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)")
        }
    }
}

1
投票

你是如何获得变量mapfragment的?在您的视图中,mapfragment似乎是MapView类型,而不是SupportMapFragment。

© www.soinside.com 2019 - 2024. All rights reserved.